Logo
~Sockets~
~Examples~
~Contact~

Look.cpp

Go to the documentation of this file.
00001 // Look.cpp
00002 // released 2006-09-25
00003 /*
00004 Copyright (C) 2006  Anders Hedstrom (grymse@alhem.net)
00005 
00006 This program is free software; you can redistribute it and/or
00007 modify it under the terms of the GNU General Public License
00008 as published by the Free Software Foundation; either version 2
00009 of the License, or (at your option) any later version.
00010 
00011 This program is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with this program; if not, write to the Free Software
00018 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 */
00020 #include <math.h>
00021 
00022 #include "Look.h"
00023 #include "InSocket.h"
00024 #include "Chunk.h"
00025 #include "Player.h"
00026 #include "Terrain.h"
00027 #include "Area.h"
00028 #include "Steps.h"
00029 
00030 
00031 void Look::Execute(InSocket *from,const std::string& params,Parse& pa)
00032 {
00033         LookMap(from);
00034         Show(from);
00035         from -> SetPrompt();
00036 }
00037 
00038 
00039 void Look::LookMap(InSocket *from)
00040 {
00041         Player pl(from -> GetDatabase(),from -> GetAccountName());
00042         if (pl.IsWizView() && pl.IsWizard())
00043         {
00044                 WizMap(from);
00045         }
00046         else
00047         {
00048                 NormalMap(from);
00049         }
00050 }
00051 
00052 
00053 void Look::WizMap(InSocket *from)
00054 {
00055         Database& db = from -> GetDatabase();
00056         Player pl(db, from -> GetAccountName());
00057         Area area(db, pl.GetArea());
00058         Chunk chunk(db, area, pl.GetChunk());
00059 
00060         std::string mapstr;
00061         std::string colorstr;
00062         for (int y = 0; y < area.GetHeight(); y++)
00063         {
00064                 for (int x = 0; x < area.GetWidth(); x++)
00065                 {
00066                         if (x == pl.GetChunkX() && y == pl.GetChunkY())
00067                         {
00068                                 mapstr += "&nA";
00069                                 colorstr = "&n";
00070                         }
00071                         else
00072                         {
00073                                 Terrain t(db, area, chunk.Get(x, y));
00074                                 if (t.Exists())
00075                                 {
00076                                         if (t.GetColor() != colorstr)
00077                                         {
00078                                                 mapstr += t.GetColor();
00079                                                 colorstr = t.GetColor();
00080                                         }
00081                                         mapstr += t.GetChar();
00082                                 }
00083                                 else
00084                                 {
00085                                         mapstr += ' ';
00086                                 }
00087                         }
00088                 }
00089                 mapstr += "&n\n";
00090                 colorstr = "&n";
00091         }
00092         from -> Send(mapstr);
00093         //Show(from);
00094 }
00095 
00096 
00097 void Look::NormalMap(InSocket *from)
00098 {
00099         Database& db = from -> GetDatabase();
00100         Player pl(db, from -> GetAccountName());
00101         Area area(db, pl.GetArea());
00102         Chunk chunk(db, area, pl.GetChunk());
00103         int cx = pl.GetChunkX();
00104         int cy = pl.GetChunkY();
00105         double sightrange = pl.SightRange();
00106         int xr = (int)(3 * sightrange / 2) + 1;
00107         int yr = (int)sightrange + 1;
00108 
00109         long xcoord = pl.GetCoordX(area, chunk);
00110         long ycoord = pl.GetCoordY(area, chunk);
00111         Steps st(db, pl, area, chunk.GetZ(), ycoord);
00112         st.AddCoord(xcoord);
00113 
00114         std::string mapstr;
00115         std::string colorstr;
00116 
00117         for (int y = -yr; y <= yr; y++)
00118         {
00119                 for (int x = -xr; x <= xr; x++)
00120                 {
00121                         int xtmp = 2 * x / 3;
00122                         if (sqrt(xtmp * xtmp + y * y) >= sightrange)
00123                         {
00124                                 mapstr += "&n ";
00125                                 colorstr = "&n";
00126                         }
00127                         else
00128                         if (cx + x == pl.GetChunkX() && cy + y == pl.GetChunkY())
00129                         {
00130                                 mapstr += "&nA";
00131                                 colorstr = "&n";
00132                         }
00133                         else
00134                         {
00135                                 bool can_see = check_facing(x, y, pl.GetFacing());
00136                                 getmap(db, area, chunk, cx + x, cy + y, mapstr, colorstr, can_see);
00137                         }
00138                 }
00139                 mapstr += "&n\n";
00140                 colorstr = "&n";
00141         }
00142 
00143         from -> Send(mapstr);
00144         //Show(from);
00145 }
00146 
00147 
00148 void Look::getmap(Database& db,Area& area,Chunk& chunk,int x,int y,std::string& mapstr,std::string& colorstr,bool color)
00149 {
00150         if (x >= 0 && x < area.GetWidth() && y >= 0 && y < area.GetHeight())
00151         {
00152                 Terrain t(db, area, chunk.Get(x, y));
00153                 if (t.Exists())
00154                 {
00155                         if (!color)
00156                         {
00157                                 if (colorstr != "&n")
00158                                 {
00159                                         mapstr += "&n";
00160                                         colorstr = "&n";
00161                                 }
00162                         }
00163                         else
00164                         if (t.GetColor() != colorstr)
00165                         {
00166                                 mapstr += t.GetColor();
00167                                 colorstr = t.GetColor();
00168                         }
00169                         mapstr += t.GetChar();
00170                 }
00171                 else
00172                 {
00173                         if (colorstr != "&n")
00174                         {
00175                                 mapstr += "&n";
00176                                 colorstr = "&n";
00177                         }
00178                         mapstr += ' ';
00179                 }
00180         }
00181         else
00182         {
00183                 int chunk_x = chunk.GetX();
00184                 int chunk_y = chunk.GetY();
00185                 int chunk_z = chunk.GetZ();
00186                 while (x < 0)
00187                 {
00188                         chunk_x -= 1; // west
00189                         x += area.GetWidth();
00190                 }
00191                 while (x >= area.GetWidth())
00192                 {
00193                         chunk_x += 1; // east
00194                         x -= area.GetWidth();
00195                 }
00196                 while (y < 0)
00197                 {
00198                         chunk_y += 1; // north
00199                         y += area.GetHeight();
00200                 }
00201                 while (y >= area.GetHeight())
00202                 {
00203                         chunk_y -= 1; // south
00204                         y -= area.GetHeight();
00205                 }
00206                 Chunk cc(db, area, chunk_x, chunk_y, chunk_z);
00207                 Terrain t(db, area, cc.Get(x, y));
00208                 if (cc.Exists() && t.Exists())
00209                 {
00210                         if (!color)
00211                         {
00212                                 if (colorstr != "&n")
00213                                 {
00214                                         mapstr += "&n";
00215                                         colorstr = "&n";
00216                                 }
00217                         }
00218                         else
00219                         if (t.GetColor() != colorstr)
00220                         {
00221                                 mapstr += t.GetColor();
00222                                 colorstr = t.GetColor();
00223                         }
00224                         mapstr += t.GetChar();
00225                 }
00226                 else
00227                 {
00228                         if (colorstr != "&n")
00229                         {
00230                                 mapstr += "&n";
00231                                 colorstr = "&n";
00232                         }
00233                         mapstr += ' ';
00234                 }
00235         }
00236 }
00237 
00238 
00239 void Look::Show(InSocket *from)
00240 {
00241         Database& db = from -> GetDatabase();
00242         Player pl(db, from -> GetAccountName());
00243         Area area(db, pl.GetArea());
00244         Chunk chunk(db, area, pl.GetChunk());
00245         Terrain t(db, area, chunk.Get(pl.GetChunkX(), pl.GetChunkY()));
00246         if (t.Exists())
00247                 from -> Send(t.GetColor() + t.GetName() + "&n\n");
00248         chunk.Show(from);
00249 }
00250 
00251 
00252 bool Look::check_facing(int x,int y,int facing)
00253 {
00254         if (x > -2 && x < 2 && y > -2 && y < 2)
00255                 return true;
00257         switch (facing)
00258         {
00259         case 1:
00260                 if (y <= 0)
00261                         return true;
00262                 break;
00263         case 2:
00264                 if (y >= 0)
00265                         return true;
00266                 break;
00267         case 3:
00268                 if (x >= 0)
00269                         return true;
00270                 break;
00271         case 4:
00272                 if (x <= 0)
00273                         return true;
00274                 break;
00275         }
00276         return false;
00277 }
00278 
00279 
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