Bot.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2010 Jacek Sieka, arnetheduck on gmail point com
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #include "adchpp.h"
00020 
00021 #include "Bot.h"
00022 
00023 #include "ClientManager.h"
00024 #include "SocketManager.h"
00025 
00026 namespace adchpp {
00027 
00028 // TODO replace with lambda
00029 struct BotRemover {
00030     BotRemover(Bot* bot_) : bot(bot_) { }
00031     void operator()() {
00032         bot->die();
00033     }
00034 
00035     Bot* bot;
00036 };
00037 
00038 Bot::Bot(uint32_t sid, const Bot::SendHandler& handler_) : Entity(sid), handler(handler_), disconnecting(false) {
00039     setFlag(FLAG_BOT);
00040 
00041     // Fake a CID, the script can change this if it wants to
00042     setCID(CID::generate());
00043 }
00044 
00045 void Bot::disconnect(Util::Reason reason) throw() {
00046     if(!disconnecting) {
00047         disconnecting = true;
00048 
00049         handler = SendHandler();
00050         SocketManager::getInstance()->addJob(BotRemover(this));
00051     }
00052 }
00053 
00054 void Bot::die() {
00055     ClientManager::getInstance()->removeEntity(*this);
00056     delete this;
00057 }
00058 
00059 }
00060 
Generated on Sat Nov 27 23:37:53 2010 for adchpp by  doxygen 1.6.3