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