| [1751] | 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 |  *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008 | 
|---|
 | 24 |  *   Co-authors: | 
|---|
 | 25 |  *      ... | 
|---|
 | 26 |  * | 
|---|
 | 27 |  */ | 
|---|
 | 28 |  | 
|---|
| [3214] | 29 | #include "Host.h" | 
|---|
 | 30 |  | 
|---|
| [2773] | 31 | #include <cassert> | 
|---|
| [3214] | 32 | #include <string> | 
|---|
| [1666] | 33 |  | 
|---|
| [1907] | 34 | #include "core/ConsoleCommand.h" | 
|---|
| [3214] | 35 | #include "core/ObjectList.h" | 
|---|
| [2087] | 36 | #include "ChatListener.h" | 
|---|
| [1666] | 37 |  | 
|---|
| [2171] | 38 | namespace orxonox { | 
|---|
| [1666] | 39 |  | 
|---|
| [1907] | 40 | SetConsoleCommandShortcut(Host, Chat); | 
|---|
 | 41 |  | 
|---|
| [1666] | 42 | Host *Host::instance_=0; | 
|---|
| [2087] | 43 |  | 
|---|
| [1907] | 44 | /** | 
|---|
 | 45 |  * @brief Constructor: assures that only one reference will be created and sets the pointer | 
|---|
 | 46 |  */ | 
|---|
| [1666] | 47 | Host::Host() | 
|---|
 | 48 | { | 
|---|
| [1907] | 49 |   clientID_=0; | 
|---|
| [1666] | 50 |   assert(instance_==0); | 
|---|
 | 51 |   instance_=this; | 
|---|
 | 52 | } | 
|---|
 | 53 |  | 
|---|
 | 54 |  | 
|---|
| [1907] | 55 | /** | 
|---|
 | 56 |  * @brief Destructor: resets the instance pointer to 0 | 
|---|
 | 57 |  */ | 
|---|
| [1666] | 58 | Host::~Host() | 
|---|
 | 59 | { | 
|---|
 | 60 |   instance_=0; | 
|---|
 | 61 | } | 
|---|
 | 62 |  | 
|---|
| [1907] | 63 | /** | 
|---|
 | 64 |  * This function is used to add an enetpacket to be sent to another peer | 
|---|
 | 65 |  * @param packet Packet to be added | 
|---|
 | 66 |  * @param clientID ID of the client the packet should be sent to | 
|---|
 | 67 |  * @return success? | 
|---|
 | 68 |  */ | 
|---|
| [1666] | 69 | bool Host::addPacket(ENetPacket *packet, int clientID){ | 
|---|
 | 70 |   if(instance_) | 
|---|
 | 71 |     return instance_->queuePacket(packet, clientID); | 
|---|
 | 72 |   else | 
|---|
 | 73 |     return false; | 
|---|
 | 74 | } | 
|---|
 | 75 |  | 
|---|
| [1907] | 76 | /** | 
|---|
 | 77 |  * This function returns the ID of the player | 
|---|
 | 78 |  * @return playerID | 
|---|
 | 79 |  */ | 
|---|
| [2087] | 80 | unsigned int Host::getPlayerID(){ | 
|---|
| [1666] | 81 |   if(!instance_) | 
|---|
 | 82 |     return 0; | 
|---|
| [1907] | 83 |   return instance_->clientID_; | 
|---|
| [1666] | 84 | } | 
|---|
 | 85 |  | 
|---|
| [2087] | 86 | bool Host::Chat(const std::string& message){ | 
|---|
| [1907] | 87 |   if(!instance_) | 
|---|
| [3095] | 88 |   { | 
|---|
 | 89 |     for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) | 
|---|
 | 90 |       it->incomingChat(message, 0); | 
|---|
 | 91 |     return true; | 
|---|
 | 92 |   } | 
|---|
| [1907] | 93 |   return instance_->chat(message); | 
|---|
 | 94 | } | 
|---|
| [1666] | 95 |  | 
|---|
| [2087] | 96 | bool Host::Broadcast(const std::string& message){ | 
|---|
 | 97 |   if(!instance_) | 
|---|
| [3058] | 98 |   { | 
|---|
 | 99 |     for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) | 
|---|
| [3095] | 100 |       it->incomingChat(message, CLIENTID_UNKNOWN); | 
|---|
| [3074] | 101 |     return true; | 
|---|
| [3058] | 102 |   } | 
|---|
| [3059] | 103 |   else | 
|---|
 | 104 |     return instance_->broadcast(message); | 
|---|
| [2087] | 105 | } | 
|---|
 | 106 |  | 
|---|
 | 107 | bool Host::incomingChat(const std::string& message, unsigned int playerID){ | 
|---|
| [2171] | 108 |   for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) | 
|---|
| [2087] | 109 |     it->incomingChat(message, playerID); | 
|---|
 | 110 |  | 
|---|
| [1907] | 111 |   return instance_->processChat(message, playerID); | 
|---|
 | 112 | } | 
|---|
 | 113 |  | 
|---|
| [2171] | 114 | }//namespace orxonox | 
|---|