Logo
~Sockets~
~Examples~
~Contact~


MobFactory.cpp

Go to the documentation of this file.
00001 //MobFactory.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 
00022 #include "SmallHandler.h"
00023 #include "SmallSocket.h"
00024 #include "MobFactory.h"
00025 
00026 
00027 
00028 
00029 MobFactory::MobFactory(SmallHandler& w)
00030 :m_handler(w)
00031 {
00032         m_name_beg.push_back( "naz" );
00033         m_name_beg.push_back( "mor" );
00034         m_name_beg.push_back( "gnar" );
00035         m_name_beg.push_back( "aahr" );
00036         m_name_beg.push_back( "more" );
00037         m_name_beg.push_back( "dark" );
00038         m_name_beg.push_back( "bam" );
00039         m_name_beg.push_back( "raab" );
00040         m_name_beg.push_back( "rake" );
00041         m_name_beg.push_back( "lor" );
00042         m_name_beg.push_back( "smur" );
00043 
00044         m_name_end.push_back( "guz" );
00045         m_name_end.push_back( "kill" );
00046         m_name_end.push_back( "gul" );
00047         m_name_end.push_back( "gok" );
00048         m_name_end.push_back( "tan" );
00049         m_name_end.push_back( "tok" );
00050         m_name_end.push_back( "bul" );
00051         m_name_end.push_back( "zod" );
00052         m_name_end.push_back( "zed" );
00053         m_name_end.push_back( "dor" );
00054         m_name_end.push_back( "grim" );
00055         m_name_end.push_back( "yohn" );
00056         m_name_end.push_back( "fan" );
00057 
00058         Spawn();
00059 }
00060 
00061 
00062 MobFactory::~MobFactory()
00063 {
00064         for (mob_v::iterator it = m_mobs.begin(); it != m_mobs.end(); it++)
00065         {
00066                 MOB *p = *it;
00067                 delete p;
00068         }
00069 }
00070 
00071 
00072 void MobFactory::Spawn()
00073 {
00074         int x;
00075         int y;
00076         std::string loc;
00077         std::string name;
00078 
00079         m_handler.GetWorld().GetRandomLocation(x,y,loc);
00080 
00081         name = m_name_beg[random() % m_name_beg.size()] +
00082                m_name_end[random() % m_name_end.size()];
00083         name[0] = name[0] - 32;
00084         {
00085                 std::string str;
00086                 str = name + " enters the world\n";
00087                 static_cast<SmallHandler&>(Handler()).Event(x,y,str);
00088         }
00089         MOB *p = new MOB(m_handler,x,y,name);
00090         m_mobs.push_back(p);
00091 }
00092 
00093 
00094 void MobFactory::RandomAction()
00095 {
00096         MOB *p = m_mobs[random() % m_mobs.size()];
00097         switch(random() % 10)
00098         {
00099         case 0:
00100                 p -> Create();
00101                 break;
00102         default:
00103                 p -> Move();
00104                 break;
00105         }
00106 }
00107 
00108 
00109 void MobFactory::MOB::Move()
00110 {
00111         int ny_x = m_x;
00112         int ny_y = m_y;
00113         std::string dir;
00114         std::string rdir;
00115         std::string str;
00116         bool n,s,e,w;
00117         bool open = false;
00118         m_handler.GetWorld().GetAt(m_x,m_y,str,n,s,e,w);
00119         switch(random() % 4)
00120         {
00121         case 0: // n
00122                 ny_y--;
00123                 dir = "north";
00124                 rdir = "south";
00125                 open = n;
00126                 break;
00127         case 1: // s
00128                 ny_y++;
00129                 dir = "south";
00130                 rdir = "north";
00131                 open = s;
00132                 break;
00133         case 2: // e
00134                 ny_x++;
00135                 dir = "east";
00136                 rdir = "west";
00137                 open = e;
00138                 break;
00139         case 3: // w
00140                 ny_x--;
00141                 dir = "west";
00142                 rdir = "east";
00143                 open = w;
00144                 break;
00145         }
00146         if (open && m_handler.GetWorld().FindAt(ny_x,ny_y,str))
00147         {
00148                 static_cast<SmallHandler&>(m_handler.GetWorld().Handler()).Event(m_x,m_y,m_name + " leaves " + dir + "\n");
00149                 SetNewPos(ny_x,ny_y);
00150                 static_cast<SmallHandler&>(m_handler.GetWorld().Handler()).Event(m_x,m_y,m_name + " enters from the " + rdir + "\n");
00151         }
00152 }
00153 
00154 
00155 void MobFactory::MOB::Create()
00156 {
00157         int ny_x = m_x;
00158         int ny_y = m_y;
00159         std::string dir;
00160         std::string rdir;
00161         switch(random() % 4)
00162         {
00163         case 0: // n
00164                 ny_y--;
00165                 dir = "north";
00166                 rdir = "south";
00167                 break;
00168         case 1: // s
00169                 ny_y++;
00170                 dir = "south";
00171                 rdir = "north";
00172                 break;
00173         case 2: // e
00174                 ny_x++;
00175                 dir = "east";
00176                 rdir = "west";
00177                 break;
00178         case 3: // w
00179                 ny_x--;
00180                 dir = "west";
00181                 rdir = "east";
00182                 break;
00183         }
00184         std::string str;
00185         if (!m_handler.GetWorld().FindAt(ny_x,ny_y,str))
00186         {
00187                 str = "A small cell (created by " + m_name + ")";
00188                 m_handler.GetWorld().AddAt(ny_x,ny_y,str);
00189                 m_handler.GetWorld().Open(m_x,m_y,dir);
00190                 m_handler.GetWorld().Open(ny_x,ny_y,rdir);
00191                 static_cast<SmallHandler&>(m_handler.GetWorld().Handler()).Event(m_x,m_y,m_name + " creates a cell to the " + dir + "\n");
00192                 static_cast<SmallHandler&>(m_handler.GetWorld().Handler()).Event(m_x,m_y,m_name + " leaves " + dir + "\n");
00193                 SetNewPos(ny_x,ny_y);
00194         }
00195 }
00196 
00197 
00198 void MobFactory::MOB::SetNewPos(int x,int y)
00199 {
00200         m_x = x;
00201         m_y = y;
00202 }
00203 
00204 
00205 void MobFactory::ShowNamesAt(SmallSocket *p,int x,int y,const std::string& prefix)
00206 {
00207         for (mob_v::iterator it = m_mobs.begin(); it != m_mobs.end(); it++)
00208         {
00209                 MOB *m = *it;
00210                 if (m -> m_x == x && m -> m_y == y)
00211                 {
00212                         p -> Send("  " + prefix + m -> m_name + "\n");
00213                 }
00214         }
00215 }
00216 
00217 
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