Changeset 7533 for code/trunk/src/modules/pickup/PickupManager.cc
- Timestamp:
- Oct 13, 2010, 11:35:18 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/modules/pickup/PickupManager.cc
r7531 r7533 42 42 #include "network/Host.h" 43 43 #include "network/NetworkFunction.h" 44 44 45 #include "interfaces/PickupCarrier.h" 45 46 #include "infos/PlayerInfo.h" 46 47 #include "worldentities/pawns/Pawn.h" 48 47 49 #include "CollectiblePickup.h" 48 50 #include "PickupRepresentation.h" … … 57 59 ManageScopedSingleton(PickupManager, ScopeID::Root, false); 58 60 61 // Initialization of the name of the PickupInventory GUI. 59 62 /*static*/ const std::string PickupManager::guiName_s = "PickupInventory"; 60 63 64 // Register static newtork functions that are used to communicate changes to pickups over the network, such that the PickupInventory can display the information about the pickups properly. 61 65 registerStaticNetworkFunction(PickupManager::pickupChangedUsedNetwork); 62 66 registerStaticNetworkFunction(PickupManager::pickupChangedPickedUpNetwork); … … 68 72 Constructor. Registers the PickupManager and creates the default PickupRepresentation. 69 73 */ 70 PickupManager::PickupManager() : guiLoaded_(false), pickup Index_(0), defaultRepresentation_(NULL)74 PickupManager::PickupManager() : guiLoaded_(false), pickupHighestIndex_(0), defaultRepresentation_(NULL) 71 75 { 72 76 RegisterObject(PickupManager); 73 77 78 //TODO: Only create if isMaster(). 74 79 this->defaultRepresentation_ = new PickupRepresentation(); 75 80 … … 80 85 @brief 81 86 Destructor. 82 Destroys the default PickupRepresentation .87 Destroys the default PickupRepresentation and does some cleanup. 83 88 */ 84 89 PickupManager::~PickupManager() 85 90 { 91 // Destroying the default representation. 86 92 if(this->defaultRepresentation_ != NULL) 87 93 this->defaultRepresentation_->destroy(); 88 94 89 95 this->representations_.clear(); 90 91 //TODO: Destroy properly... 96 this->representationsNetworked_.clear(); 97 98 //TODO: Destroying properly? 99 //TODO: Shouldnt these list be empty, to avoid problems when switching levels? 100 // Destroying all the PickupInventoryContainers that are still there. 101 for(std::map<uint32_t, PickupInventoryContainer*>::iterator it = this->pickupInventoryContainers_.begin(); it != this->pickupInventoryContainers_.end(); it++) 102 delete it->second; 103 this->pickupInventoryContainers_.clear(); 104 105 // Destroying all the WeakPointers that are still there. 106 for(std::map<uint32_t, WeakPtr<Pickupable>*>::iterator it = this->pickups_.begin(); it != this->pickups_.end(); it++) 107 delete it->second; 108 this->pickups_.clear(); 109 110 this->indexes_.clear(); 92 111 93 112 COUT(3) << "PickupManager destroyed." << std::endl; … … 96 115 /** 97 116 @brief 98 Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents .99 For every type of Pickupable (uniquely id netified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered.117 Registers a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents, on the server (or in standalone mode). 118 For every type of Pickupable (uniquely identified by a PickupIdentifier) there can be one (and just one) PickupRepresentation registered. 100 119 @param identifier 101 120 The PickupIdentifier identifying the Pickupable. … … 110 129 assert(representation); 111 130 112 if(!this->representations_.empty() && this->representations_.find(identifier) != this->representations_.end()) // If the Pickupable already has a Representation registered. 131 // If the list is not empty and Pickupable already has a Representation registered. 132 if(!this->representations_.empty() && this->representations_.find(identifier) != this->representations_.end()) 113 133 return false; 114 134 … … 119 139 } 120 140 121 bool PickupManager::registerRepresentation(PickupRepresentation* representation) 122 { 123 assert(representation); 124 125 if(!this->representationsNetworked_.empty() && this->representationsNetworked_.find(representation->getObjectID()) != this->representationsNetworked_.end()) // If the PickupRepresentation is already registered. 126 return false; 127 128 this->representationsNetworked_[representation->getObjectID()] = representation; 129 return true; 130 } 131 132 /** 133 @brief 134 Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents. 141 /** 142 @brief 143 Unegisters a PickupRepresentation together with the PickupIdentifier of the Pickupable the PickupRepresentation represents, on the server (or in standalone mode). 135 144 @param identifier 136 145 The PickupIdentifier identifying the Pickupable. … … 146 155 147 156 std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); 148 if(it == this->representations_.end()) // !<If the Pickupable is not registered in the first place.157 if(it == this->representations_.end()) // If the Pickupable is not registered in the first place. 149 158 return false; 150 159 … … 155 164 } 156 165 166 /** 167 @brief 168 Registers a PickupRepresentation on the host it was created. 169 @param representation 170 A pointer to the PickupRepresentation. 171 @return 172 Returns true if successful, false if not. 173 */ 174 bool PickupManager::registerRepresentation(PickupRepresentation* representation) 175 { 176 assert(representation); 177 178 // If the list is not empty and PickupRepresentation is already registered. 179 if(!this->representationsNetworked_.empty() && this->representationsNetworked_.find(representation->getObjectID()) != this->representationsNetworked_.end()) 180 return false; 181 182 this->representationsNetworked_[representation->getObjectID()] = representation; 183 return true; 184 } 185 186 /** 187 @brief 188 Unregisters a PickupRepresentation on the host it is being destroyed (which is the same host on which it was created). 189 @param representation 190 A pointer to the Pickuprepresentation. 191 @return 192 Returns true if successful, false if not. 193 */ 157 194 bool PickupManager::unregisterRepresentation(PickupRepresentation* representation) 158 195 { … … 160 197 161 198 std::map<uint32_t, PickupRepresentation*>::iterator it = this->representationsNetworked_.find(representation->getObjectID()); 162 if(it == this->representationsNetworked_.end()) // !<If the Pickupable is not registered in the first place.199 if(it == this->representationsNetworked_.end()) // If the Pickupable is not registered in the first place. 163 200 return false; 164 201 … … 175 212 Returns a pointer to the PickupRepresentation. 176 213 */ 214 //TODO: Why not return a const? 177 215 PickupRepresentation* PickupManager::getRepresentation(const PickupIdentifier* identifier) 178 216 { 179 217 std::map<const PickupIdentifier*, PickupRepresentation*, PickupIdentifierCompare>::iterator it = this->representations_.find(identifier); 180 if(it == this->representations_.end()) 218 if(it == this->representations_.end()) // If there is no PickupRepresentation associated with the input PickupIdentifier. 181 219 { 182 220 COUT(4) << "PickupManager::getRepresentation() returned default representation." << std::endl; … … 189 227 /** 190 228 @brief 191 Get the PickupRepresentation of an input Pickupable. 192 This method spares us the hassle to export the PickupIdentifier class to lua. 193 @param pickup 194 The Pickupable whose PickupRepresentation should be returned. 195 @return 196 Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred. 197 */ 198 orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(uint64_t pickup) 199 { 200 this->representationsNetworked_.clear(); 201 for(ObjectList<PickupRepresentation>::iterator it = ObjectList<PickupRepresentation>::begin(); it != ObjectList<PickupRepresentation>::end(); ++it) 202 this->representationsNetworked_[it->getObjectID()] = *it; 203 204 std::map<uint64_t, PickupInventoryContainer*>::iterator it = this->pickupInventoryContainers_.find(pickup); 205 if(it == this->pickupInventoryContainers_.end()) 206 return this->defaultRepresentation_; 207 208 std::map<uint32_t, PickupRepresentation*>::iterator it2 = this->representationsNetworked_.find(it->second->representationObjectId); 209 if(it2 == this->representationsNetworked_.end()) 210 return this->defaultRepresentation_; 211 212 return it2->second; 213 } 214 215 /** 216 @brief 217 Update the list of picked up Pickupables and get the number of Pickupables in the list. 218 This method is used in lua to populate the PickupInventory. The intended usage is to call this method to update the list of Pickupables and then use popPickup() to get the individual Pickupables. 219 @return 220 Returns the number of the players picked up Pickupables. 221 */ 222 int PickupManager::getNumPickups(void) 223 { 224 this->pickupsIterator_ = this->pickupInventoryContainers_.begin(); 225 return this->pickupInventoryContainers_.size(); 226 } 227 228 /** 229 @brief 230 Drop the input Pickupable. 231 This method checks whether the inout Pickupable still exists and drops it, if so. 232 @param pickup 233 The Pickupable to be dropped. 234 */ 235 void PickupManager::dropPickup(uint64_t pickup) 236 { 237 if(GameMode::isMaster() && !this->pickups_.empty()) 238 { 239 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 240 if(pickupable != NULL) 241 pickupable->drop(); 242 } 243 else 244 { 245 callStaticNetworkFunction(PickupManager::dropPickupNetworked, 0, pickup); 246 } 247 } 248 249 /*static*/ void PickupManager::dropPickupNetworked(uint64_t pickup) 250 { 251 if(GameMode::isServer()) 252 { 253 PickupManager& manager = PickupManager::getInstance(); 254 if(manager.pickups_.empty()) 255 return; 256 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 257 if(pickupable != NULL) 258 pickupable->drop(); 259 } 260 } 261 262 /** 263 @brief 264 Use (or unuse) the input Pickupable. 265 This method checks whether the input Pickupable still exists and uses (or unuses) it, if so, 266 @param pickup 267 The Pickupable to be used (or unused). 268 @param use 269 If true the input Pickupable is used, if false it is unused. 270 */ 271 void PickupManager::usePickup(uint64_t pickup, bool use) 272 { 273 if(GameMode::isMaster() && !this->pickups_.empty()) 274 { 275 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 276 if(pickupable != NULL) 277 pickupable->setUsed(use); 278 } 279 else 280 { 281 callStaticNetworkFunction(PickupManager::usePickupNetworked, 0, pickup, use); 282 } 283 } 284 285 /*static*/ void PickupManager::usePickupNetworked(uint64_t pickup, bool use) 286 { 287 if(GameMode::isServer()) 288 { 289 PickupManager& manager = PickupManager::getInstance(); 290 if(manager.pickups_.empty()) 291 return; 292 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 293 if(pickupable != NULL) 294 pickupable->setUsed(use); 295 } 296 } 297 298 /** 299 @brief 300 Check whether the input Pickupable is valid, meaning that it is in the PickupManager's list and still exists. 301 @param pickup 302 The Pickupable. 303 @return 304 Returns true if the input Pickupable is still valid, false if not. 305 */ 306 bool PickupManager::isValidPickup(uint64_t pickup) 307 { 308 return this->pickups_.find(pickup) != this->pickups_.end(); 309 } 310 229 Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input used state. 230 @param pickup 231 The Pickupable whose used status changed. 232 @param used 233 The used status the Pickupable changed to. 234 */ 311 235 void PickupManager::pickupChangedUsed(Pickupable* pickup, bool used) 312 236 { … … 317 241 318 242 CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup); 319 // If the Pic upable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.243 // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is. 320 244 if(collectible != NULL && collectible->isInCollection()) 321 245 return; 322 246 247 // Getting clientId of the host this change of the pickup's used status concerns. 323 248 PickupCarrier* carrier = pickup->getCarrier(); 324 249 while(carrier->getCarrierParent() != NULL) … … 332 257 unsigned int clientId = info->getClientID(); 333 258 334 std::map<Pickupable*, uint64_t>::iterator it = this->indexes_.find(pickup); 259 // Get the number identifying the pickup. 260 std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup); 335 261 assert(it != this->indexes_.end()); 336 337 uint64_t index = it->second; 262 uint32_t index = it->second; 263 264 // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns. 338 265 if(GameMode::isStandalone() || Host::getPlayerID() == clientId) 339 266 { 340 267 PickupManager::pickupChangedUsedNetwork(index, used, pickup->isUsable(), pickup->isUnusable()); 341 268 } 269 // If the concerned host is somewhere in the network, we call pickupChangedUsedNetwork() on its PickupManager. 342 270 else 343 271 { … … 346 274 } 347 275 348 /*static*/ void PickupManager::pickupChangedUsedNetwork(uint64_t pickup, bool inUse, bool usable, bool unusable) 349 { 350 PickupManager& manager = PickupManager::getInstance(); 276 /** 277 @brief 278 Helper method to react to the change in the used status of a Pickupable. 279 Static method that is used by the server to inform the client it concerns about the status change. 280 The parameters that are given are used to update the information (i.e. the PickupInventoryContainer) the concerning PickupManager has about the Pickupable that changed. 281 @param pickup 282 A number identifying the Pickupable that changed its used status. 283 @param inUse 284 The used status the Pickupable changed to. (i.e. whether the Pickupable is in use or not). 285 @param usable 286 Whether the Pickupable's used status can be changed used in the PickupInventory. 287 @param unusable 288 Whether the Pickupable's used status can be changed to unused in the PickupInventory. 289 */ 290 /*static*/ void PickupManager::pickupChangedUsedNetwork(uint32_t pickup, bool inUse, bool usable, bool unusable) 291 { 292 PickupManager& manager = PickupManager::getInstance(); // Get the PickupManager singleton on this host. 293 // If the input Pickupable (i.e its identifier) is not present in the list the PickupManager has. 351 294 if(manager.pickupInventoryContainers_.find(pickup) == manager.pickupInventoryContainers_.end()) 352 295 { … … 355 298 } 356 299 300 // Update the Pickupable's container with the information transferred. 357 301 manager.pickupInventoryContainers_[pickup]->inUse = inUse; 358 302 manager.pickupInventoryContainers_[pickup]->usable = usable; 359 303 manager.pickupInventoryContainers_[pickup]->unusable = unusable; 360 304 361 manager.updateGUI(); 362 } 363 305 manager.updateGUI(); // Tell the PickupInventory that something has changed. 306 } 307 308 /** 309 @brief 310 Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input pickedUp state. 311 @param pickup 312 The Pickupable whose pickedUp status changed. 313 @param pickedUp 314 The pickedUp status the Pickupable changed to. 315 */ 364 316 void PickupManager::pickupChangedPickedUp(Pickupable* pickup, bool pickedUp) 365 317 { 366 318 assert(pickup); 367 319 368 if(!GameMode::isMaster()) 320 if(!GameMode::isMaster()) // If this is neither standalone nor the server. 369 321 return; 370 322 371 323 CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup); 372 // If the Pic upable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.324 // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is. 373 325 if(collectible != NULL && collectible->isInCollection()) 374 326 return; 375 327 328 // Getting clientId of the host this change of the pickup's pickedUp status concerns. 376 329 PickupCarrier* carrier = pickup->getCarrier(); 377 330 while(carrier->getCarrierParent() != NULL) … … 380 333 if(pawn == NULL) 381 334 return; 382 PlayerInfo* info = pawn->get Player();335 PlayerInfo* info = pawn->getFormerPlayer(); 383 336 if(info == NULL) 384 337 return; 385 338 unsigned int clientId = info->getClientID(); 386 339 387 uint64_t index = 0; 388 if(pickedUp) 389 { 390 index = this->getPickupIndex(); 340 uint32_t index = 0; 341 if(pickedUp) // If the Pickupable has changed to picked up, it is added to the required lists. 342 { 343 index = this->getPickupIndex(); // Ge a new identifier (index) for the Pickupable. 344 // Add the Pickupable to the indexes_ and pickups_ lists. 391 345 this->indexes_[pickup] = index; 392 346 this->pickups_[index] = new WeakPtr<Pickupable>(pickup); 393 347 } 394 else 395 { 396 std::map<Pickupable*, uint64_t>::iterator it = this->indexes_.find(pickup); 348 else // If it was dropped, it is removed from the required lists. 349 { 350 // Get the indentifier (index) that identifies the input Pickupable. 351 std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup); 397 352 index = it->second; 353 354 // Remove the Pickupable form the indexes_ and pickups_ list. 398 355 WeakPtr<Pickupable>* ptr = this->pickups_[index]; 399 356 this->indexes_.erase(it); … … 402 359 } 403 360 361 // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns. 362 //TODO: Needs to be added to server even if is was not picked up by it? 404 363 if(GameMode::isStandalone() || Host::getPlayerID() == clientId) 405 364 { 365 // If there is no PickupRepresentation registered the default representation is used. 406 366 if(this->representations_.find(pickup->getPickupIdentifier()) == this->representations_.end()) 407 367 PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->defaultRepresentation_->getObjectID(), pickedUp); … … 409 369 PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->representations_[pickup->getPickupIdentifier()]->getObjectID(), pickedUp); 410 370 } 371 // If the concerned host is somewhere in the network, we call pickupChangedPickedUpNetwork() on its PickupManager. 411 372 else 412 373 { 374 // If there is no PickupRepresentation registered the default representation is used. 413 375 if(this->representations_.find(pickup->getPickupIdentifier()) == this->representations_.end()) 414 376 { … … 423 385 } 424 386 425 /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint64_t pickup, bool usable, uint32_t representationObjectId, bool pickedUp) 426 { 427 PickupManager& manager = PickupManager::getInstance(); 387 /** 388 @brief 389 Helper method to react to the change in the pickedUp status of a Pickupable. 390 Static method that is used by the server to inform the client it concerns about the status change. 391 The parameters that are given are used to update the information (i.e. the PickupInventoryContainer) the concerning PickupManager has about the Pickupable that changed. 392 @param pickup 393 A number identifying the Pickupable that changed its pickedUp status. 394 @param unusable 395 Whether the Pickupable's used status can be changed to unused in the PickupInventory. 396 @param representationObjectId 397 The objectId identifying (over the network) the PickupRepresentation that represents this Pickupable. 398 @param pickedUp 399 The pickedUp status the Pickupable changed to. 400 */ 401 /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint32_t pickup, bool usable, uint32_t representationObjectId, bool pickedUp) 402 { 403 PickupManager& manager = PickupManager::getInstance(); // Get the PickupManager singleton on this host. 404 // If the Pickupable has been picked up, we create a new PickupInventoryContainer for it. 428 405 if(pickedUp) 429 406 { 407 // Create a new PickupInventoryContainer for the Pickupable and set all the necessary information. 430 408 PickupInventoryContainer* container = new PickupInventoryContainer; 431 409 container->pickup = pickup; … … 435 413 container->unusable = false; 436 414 container->representationObjectId = representationObjectId; 437 manager.pickupInventoryContainers_.insert(std::pair<uint64_t, PickupInventoryContainer*>(pickup, container)); 438 439 if(GameMode::showsGraphics()) 440 { 441 if(!manager.guiLoaded_) 442 { 443 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 444 manager.guiLoaded_ = true; 445 } 446 GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()"); 447 } 448 } 415 // Insert the container into the pickupInventoryContainers_ list. 416 manager.pickupInventoryContainers_.insert(std::pair<uint32_t, PickupInventoryContainer*>(pickup, container)); 417 418 manager.updateGUI(); // Tell the PickupInventory that something has changed. 419 } 420 // If the Pickupable has been dropped, we remove it from the pickupInventoryContainers_ list. 449 421 else 450 422 { 451 std::map<uint 64_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup);423 std::map<uint32_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup); 452 424 if(it != manager.pickupInventoryContainers_.end()) 453 425 delete it->second; 454 426 manager.pickupInventoryContainers_.erase(pickup); 455 427 456 manager.updateGUI(); 457 } 458 } 459 428 manager.updateGUI(); // Tell the PickupInventory that something has changed. 429 } 430 } 431 432 /** 433 @brief 434 Get the PickupRepresentation of an input Pickupable. 435 This method spares us the hassle to export the PickupIdentifier class to lua. 436 @param pickup 437 The number identifying the Pickupable whose PickupRepresentation should be returned. 438 @return 439 Returns the PickupRepresentation of the input Pickupable or NULL if an error occurred. 440 */ 441 orxonox::PickupRepresentation* PickupManager::getPickupRepresentation(uint32_t pickup) 442 { 443 // Clear and rebuild the representationsNetworked_ list. 444 //TODO: Better solution? 445 this->representationsNetworked_.clear(); 446 for(ObjectList<PickupRepresentation>::iterator it = ObjectList<PickupRepresentation>::begin(); it != ObjectList<PickupRepresentation>::end(); ++it) 447 this->representationsNetworked_[it->getObjectID()] = *it; 448 449 // Get the container belonging to the input pickup, if not found return the default representation. 450 std::map<uint32_t, PickupInventoryContainer*>::iterator it = this->pickupInventoryContainers_.find(pickup); 451 if(it == this->pickupInventoryContainers_.end()) 452 return this->defaultRepresentation_; 453 454 // Get the PickupRepresentation of the input pickup (through the objecId of the representation stored in the PickupInventoryContainer belonging to the pickup), if not found return the default representation. 455 std::map<uint32_t, PickupRepresentation*>::iterator it2 = this->representationsNetworked_.find(it->second->representationObjectId); 456 if(it2 == this->representationsNetworked_.end()) 457 return this->defaultRepresentation_; 458 459 return it2->second; 460 } 461 462 /** 463 @brief 464 Get the number of pickups currently picked up by the player. 465 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. 466 @return 467 Returns the number of the players picked up Pickupables. 468 */ 469 int PickupManager::getNumPickups(void) 470 { 471 this->pickupsIterator_ = this->pickupInventoryContainers_.begin(); // Reset iterator. 472 473 return this->pickupInventoryContainers_.size(); 474 } 475 476 /** 477 @brief 478 Drop the input Pickupable. 479 This method checks whether the input Pickupable still exists and drops it, if so. 480 @param pickup 481 The identifier of the Pickupable to be dropped. 482 */ 483 void PickupManager::dropPickup(uint32_t pickup) 484 { 485 // If we're either server or standalone and the list of pickups is not empty, we find and drop the input pickup. 486 if(GameMode::isMaster() && !this->pickups_.empty()) 487 { 488 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 489 if(pickupable != NULL) 490 pickupable->drop(); 491 } 492 // If we're neither server nor standalone we drop the pickup by calling dropPickupNetworked() of the PickupManager on the server. 493 else 494 { 495 callStaticNetworkFunction(PickupManager::dropPickupNetworked, 0, pickup); 496 } 497 } 498 499 /** 500 @brief 501 Helper method to drop the input pickup on the server. 502 Static method that is used by clients to instruct the server to drop the input pickup. 503 @param pickup 504 The identifier of the Pickupable to be dropped. 505 */ 506 /*static*/ void PickupManager::dropPickupNetworked(uint32_t pickup) 507 { 508 if(GameMode::isServer()) // Obviously we only want to do this on the server. 509 { 510 PickupManager& manager = PickupManager::getInstance(); 511 //TODO: Just call dropPickup() on manager? 512 if(manager.pickups_.empty()) 513 return; 514 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 515 if(pickupable != NULL) 516 pickupable->drop(); 517 } 518 } 519 520 /** 521 @brief 522 Use (or unuse) the input Pickupable. 523 This method checks whether the input Pickupable still exists and uses (or unuses) it, if so, 524 @param pickup 525 The identifier of the Pickupable to be used (or unused). 526 @param use 527 If true the input Pickupable is used, if false it is unused. 528 */ 529 void PickupManager::usePickup(uint32_t pickup, bool use) 530 { 531 // 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. 532 if(GameMode::isMaster() && !this->pickups_.empty()) 533 { 534 Pickupable* pickupable = this->pickups_.find(pickup)->second->get(); 535 if(pickupable != NULL) 536 pickupable->setUsed(use); 537 } 538 // If we're neither server nor standalone we change the used status of the pickup by calling usePickupNetworked() of the PickupManager on the server. 539 else 540 { 541 callStaticNetworkFunction(PickupManager::usePickupNetworked, 0, pickup, use); 542 } 543 } 544 545 /** 546 @brief 547 Helper method to use (or unuse) the input Pickupable on the server. 548 Static method that is used by clients to instruct the server to use (or unuse) the input pickup. 549 @param pickup 550 The identifier of the Pickupable to be used (or unused). 551 @param use 552 If true the input Pickupable is used, if false it is unused. 553 */ 554 /*static*/ void PickupManager::usePickupNetworked(uint32_t pickup, bool use) 555 { 556 if(GameMode::isServer()) 557 { 558 PickupManager& manager = PickupManager::getInstance(); 559 //TODO: Just call usePickup() on manager? 560 if(manager.pickups_.empty()) 561 return; 562 Pickupable* pickupable = manager.pickups_.find(pickup)->second->get(); 563 if(pickupable != NULL) 564 pickupable->setUsed(use); 565 } 566 } 567 568 /** 569 @brief 570 Updates the PickupInventory GUI. 571 Also loads the PickupInventory GUI if is hasn't been done already. 572 */ 460 573 inline void PickupManager::updateGUI(void) 461 574 { 575 // We only need to update (and load) the GUI if this host shows graphics. 462 576 if(GameMode::showsGraphics()) 463 577 { 464 if(!this->guiLoaded_) 578 if(!this->guiLoaded_) // If the GUI hasn't been loaded, yet, we load it. 465 579 { 466 580 GUIManager::getInstance().loadGUI(PickupManager::guiName_s); 467 581 this->guiLoaded_ = true; 468 582 } 583 584 // Update the GUI. 469 585 GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()"); 470 586 } 471 587 } 472 588 473 uint64_t PickupManager::getPickupIndex(void) 474 { 475 if(this->pickupIndex_ == uint64_t(~0x0)-1) 476 this->pickupIndex_ = 0; 477 return this->pickupIndex_++; 589 /** 590 @brief 591 Get a new index for a Pickupable. 592 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. 593 @return 594 Returns the new index. 595 */ 596 uint32_t PickupManager::getPickupIndex(void) 597 { 598 if(this->pickupHighestIndex_ == uint32_t(~0x0)-1) // If we've reached the highest possible number, we wrap around. 599 this->pickupHighestIndex_ = 0; 600 return this->pickupHighestIndex_++; 478 601 } 479 602
Note: See TracChangeset
for help on using the changeset viewer.