Logo
~Sockets~
~Examples~
~Contact~


SmallHandler.cpp

Go to the documentation of this file.
00001 //SmallHandler.cpp
00002 /*
00003 Copyright (C) 2004  Anders Hedstrom
00004 
00005 This program is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU General Public License
00007 as published by the Free Software Foundation; either version 2
00008 of the License, or (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with this program; if not, write to the Free Software
00017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 */
00019 
00020 //#include <stdio.h>
00021 #include <Utility.h>
00022 #include "SmallSocket.h"
00023 #include "ItemFactory.h"
00024 #include "SmallHandler.h"
00025 
00026 
00027 SmallHandler::SmallHandler()
00028 :SocketHandler()
00029 ,m_world(*this)
00030 ,m_mob(*this)
00031 ,m_itemf(*this)
00032 {
00033         m_persist.push_back(&m_pf);
00034 }
00035 
00036 
00037 SmallHandler::~SmallHandler()
00038 {
00039 }
00040 
00041 
00042 void SmallHandler::Who(SmallSocket *p2)
00043 {
00044         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00045         {
00046                 Socket *p0 = (*it).second;
00047                 SmallSocket *p = dynamic_cast<SmallSocket *>(p0);
00048                 if (p)
00049                 {
00050                         p2 -> Send(p -> GetName() + "\n");
00051                 }
00052         }
00053 }
00054 
00055 
00056 void SmallHandler::SecTick(time_t t)
00057 {
00058         if (random() % 5 == 0)
00059         {
00060                 m_itemf.Spawn();
00061         }
00062         else
00063         if (random() % 120 == 0)
00064         {
00065                 m_mob.Spawn();
00066         }
00067         else
00068         if (random() % 5 == 0)
00069         {
00070                 m_mob.RandomAction();
00071         }
00072 }
00073 
00074 
00075 void SmallHandler::ShowCell(SmallSocket *p)
00076 {
00077         int x = p -> GetX();
00078         int y = p -> GetY();
00079         std::string name;
00080         bool n,s,e,w;
00081         m_world.GetAt(x,y,name,n,s,e,w);
00082         p -> Send(name + "\n");
00083         m_mob.ShowNamesAt(p,x,y,"Here is ");
00084         m_itemf.ShowNamesAt(p,x,y,"You see ");
00085 /*
00086         if (n && m_world.FindAt(x,y - 1,name))
00087         {
00088                 p -> Send("To the north you see... " + name + "\n");
00089                 m_mob.ShowNamesAt(p,x,y - 1);
00090                 m_itemf.ShowNamesAt(p,x,y - 1);
00091         }
00092         if (s && m_world.FindAt(x,y + 1,name))
00093         {
00094                 p -> Send("To the south you see... " + name + "\n");
00095                 m_mob.ShowNamesAt(p,x,y + 1);
00096                 m_itemf.ShowNamesAt(p,x,y + 1);
00097         }
00098         if (e && m_world.FindAt(x + 1,y,name))
00099         {
00100                 p -> Send("To the east you see... " + name + "\n");
00101                 m_mob.ShowNamesAt(p,x + 1,y);
00102                 m_itemf.ShowNamesAt(p,x + 1,y);
00103         }
00104         if (w && m_world.FindAt(x - 1,y,name))
00105         {
00106                 p -> Send("To the west you see... " + name + "\n");
00107                 m_mob.ShowNamesAt(p,x - 1,y);
00108                 m_itemf.ShowNamesAt(p,x - 1,y);
00109         }
00110 */
00111         std::string str = "Exits:&W";
00112         str += n ? " N" : "";
00113         str += s ? " S" : "";
00114         str += e ? " E" : "";
00115         str += w ? " W" : "";
00116         p -> Send( str + "&n\n");
00117 }
00118 
00119 
00120 void SmallHandler::Event(int x,int y,const std::string& line,SmallSocket *from,bool bAll)
00121 {
00122         for (socket_m::iterator it = m_sockets.begin(); it != m_sockets.end(); it++)
00123         {
00124                 Socket *p0 = (*it).second;
00125                 SmallSocket *p = dynamic_cast<SmallSocket *>(p0);
00126                 if (p && p != from)
00127                 {
00128                         if (bAll || p -> IsAt(x,y))
00129                         {
00130                                 p -> Send("\n" + line);
00131                                 p -> SendPrompt();
00132                         }
00133                 }
00134         }
00135 }
00136 
00137 
00138 void SmallHandler::Load()
00139 {
00140         for (ipersist_v::iterator it = m_persist.begin(); it != m_persist.end(); it++)
00141         {
00142                 IPersist *p = *it;
00143                 p -> Load();
00144         }
00145 }
00146 
00147 
00148 void SmallHandler::ShowStatus(SmallSocket *sendto)
00149 {
00150         sendto -> Send("Number of rooms : &R" + Utility::l2string(m_world.NumberOfCells()) + "&n\n");
00151         sendto -> Send("Number of mobs  : &G" + Utility::l2string(m_mob.NumberOfMobs()) + "&n\n");
00152         sendto -> Send("Number of items : &B" + Utility::l2string(m_itemf.NumberOfItems()) + "&n\n");
00153 }
00154 
00155 
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4