Logo
~Sockets~
~Examples~
~Contact~

Player.cpp

Go to the documentation of this file.
00001 // Player.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 <libfuture.h>
00021 #include <Utility.h>
00022 #include <Parse.h>
00023 #include <math.h>
00024 
00025 #include "Player.h"
00026 #include "Chunk.h"
00027 #include "Area.h"
00028 #include "Steps.h"
00029 #include "Race.h"
00030 
00031 
00032 // statics
00033 accounts_t Player::m_accounts;
00034 
00035 
00036 Player::Player(Database& db,const std::string& name) : Object(db)
00037 , m_account(Player::GetAccount(db, name))
00038 {
00039 }
00040 
00041 
00042 Player::~Player()
00043 {
00044 }
00045 
00046 
00047 db::Account *Player::GetAccount(Database& db,const std::string& name)
00048 {
00049         db::Account *p = m_accounts[name];
00050         if (p != NULL)
00051                 return p;
00052         p = new db::Account(db, name);
00053         p -> name = name;
00054         if (!p -> num)
00055         {
00056                 Query q(db);
00057                 long nr = q.get_count("select count(*) from account");
00058                 p -> wizard = !nr;
00059         }
00060         m_accounts[name] = p;
00061         return p;
00062 }
00063 
00064 
00065 void Player::Save()
00066 {
00067         if (m_account)
00068                 m_account -> save();
00069 }
00070 
00071 
00072 bool Player::Exists()
00073 {
00074         if (m_account && m_account -> num)
00075                 return true;
00076         return false;
00077 }
00078 
00079 
00080 std::string Player::GetName()
00081 {
00082         return m_account ? m_account -> name: "";
00083 }
00084 
00085 
00086 bool Player::IsWizard()
00087 {
00088         return m_account ? (m_account -> wizard ? true : false) : false;
00089 }
00090 
00091 
00092 std::string Player::GetPasswd()
00093 {
00094         return m_account -> passwd;
00095 }
00096 
00097 
00098 std::string Player::GetDisplayName()
00099 {
00100         return m_account -> display_name + "&n";
00101 }
00102 
00103 
00104 void Player::VerifyPos()
00105 {
00106         if (!m_account -> area)
00107         {
00108                 Area area(m_db, "World");
00109                 Chunk chunk(m_db, area, 1000, 1000, 0); // default starting point
00110                 m_account -> area = area.GetNum();
00111                 m_account -> chunk = chunk.GetNum();
00112                 m_account -> chunkx = area.GetWidth() / 2;
00113                 m_account -> chunky = area.GetHeight() / 2;
00114                 m_account -> save();
00115         }
00116         if (!m_account -> facing)
00117         {
00118                 m_account -> facing = 1;
00119                 Save();
00120         }
00121 }
00122 
00123 
00124 void Player::SetName(const std::string& x)
00125 {
00126         m_account -> name = x;
00127 }
00128 
00129 
00130 void Player::SetPasswd(const std::string& x)
00131 {
00132         m_account -> passwd = x;
00133 }
00134 
00135 
00136 void Player::SetWizard(bool x)
00137 {
00138         m_account -> wizard = x ? 1 : 0;
00139 }
00140 
00141 
00142 void Player::SetDisplayName(const std::string& x)
00143 {
00144         m_account -> display_name = x;
00145 }
00146 
00147 
00148 void Player::SetEmail(const std::string& x)
00149 {
00150         m_account -> email = x;
00151 }
00152 
00153 
00154 long Player::GetArea()
00155 {
00156         return m_account -> area;
00157 }
00158 
00159 
00160 long Player::GetChunk()
00161 {
00162         return m_account -> chunk;
00163 }
00164 
00165 
00166 long Player::GetChunkX()
00167 {
00168         return m_account -> chunkx;
00169 }
00170 
00171 
00172 long Player::GetChunkY()
00173 {
00174         return m_account -> chunky;
00175 }
00176 
00177 
00178 void Player::SetChunkX(int x)
00179 {
00180         m_account -> chunkx = x;
00181 }
00182 
00183 
00184 void Player::SetChunkY(int y)
00185 {
00186         m_account -> chunky = y;
00187 }
00188 
00189 
00190 void Player::SetArea(long x)
00191 {
00192         m_account -> area = x;
00193 }
00194 
00195 
00196 void Player::SetChunk(long x)
00197 {
00198         m_account -> chunk = x;
00199 }
00200 
00201 
00202 long Player::GetNum()
00203 {
00204         return m_account -> num;
00205 }
00206 
00207 
00208 int Player::GetFacing()
00209 {
00210         return m_account -> facing;
00211 }
00212 
00213 
00214 void Player::SetFacing(int x)
00215 {
00216         m_account -> facing = x;
00217 }
00218 
00219 
00220 void Player::SetWizView(bool x)
00221 {
00222         m_account -> wizview = x ? 1 : 0;
00223 }
00224 
00225 
00226 bool Player::IsWizView()
00227 {
00228         return (m_account -> wizview && m_account -> wizard) ? true : false;
00229 }
00230 
00231 
00232 long Player::GetCoordX(Area& area,Chunk& chunk)
00233 {
00234         return chunk.GetX() * area.GetWidth() + GetChunkX();
00235 }
00236 
00237 
00238 long Player::GetCoordY(Area& area,Chunk& chunk)
00239 {
00240         return chunk.GetY() * area.GetHeight() + area.GetHeight() - GetChunkY() - 1;
00241 }
00242 
00243 
00244 bool Player::Seen(Area& area,long xcoord,long ycoord,int ch_z)
00245 {
00246         for (int y = 0; y < SightRange() + 1; y++)
00247         {
00248                 db::Steps *st;
00249                 if (y)
00250                 {
00251                         st = Steps::GetStep(*this, area, ch_z, ycoord - y);
00252                         if (st)
00253                         {
00254                                 Parse pa(st -> xcoords, ":");
00255                                 std::string tmp = pa.getword();
00256                                 while (tmp.size())
00257                                 {
00258                                         long x = atol(tmp.c_str());
00259                                         long x0 = x - xcoord;
00260                                         long y0 = st -> ycoord - ycoord;
00261                                         long xtmp = 2 * x0 / 3;
00262                                         if (sqrt(xtmp * xtmp + y0 * y0) < SightRange())
00263                                         {
00264                                                 return true;
00265                                         }
00266                                         //
00267                                         tmp = pa.getword();
00268                                 }
00269                         }
00270                         st = Steps::GetStep(*this, area, ch_z, ycoord + y);
00271                         if (st)
00272                         {
00273                                 Parse pa(st -> xcoords, ":");
00274                                 std::string tmp = pa.getword();
00275                                 while (tmp.size())
00276                                 {
00277                                         long x = atol(tmp.c_str());
00278                                         long x0 = x - xcoord;
00279                                         long y0 = st -> ycoord - ycoord;
00280                                         long xtmp = 2 * x0 / 3;
00281                                         if (sqrt(xtmp * xtmp + y0 * y0) < SightRange())
00282                                         {
00283                                                 return true;
00284                                         }
00285                                         //
00286                                         tmp = pa.getword();
00287                                 }
00288                         }
00289                 }
00290                 else
00291                 {
00292                         st = Steps::GetStep(*this, area, ch_z, ycoord);
00293                         if (st)
00294                         {
00295                                 Parse pa(st -> xcoords, ":");
00296                                 std::string tmp = pa.getword();
00297                                 while (tmp.size())
00298                                 {
00299                                         long x = atol(tmp.c_str());
00300                                         long x0 = x - xcoord;
00301                                         long y0 = st -> ycoord - ycoord;
00302                                         long xtmp = 2 * x0 / 3;
00303                                         if (sqrt(xtmp * xtmp + y0 * y0) < SightRange())
00304                                         {
00305                                                 return true;
00306                                         }
00307                                         //
00308                                         tmp = pa.getword();
00309                                 }
00310                         }
00311                 }
00312         }
00313         return false;
00314 }
00315 
00316 
00317 double Player::SightRange()
00318 {
00319         Race r(m_db, m_account -> race);
00320         if (r.Exists())
00321                 return r.GetSightRange();
00322         return 3.5;
00323 }
00324 
00325 
00326 void Player::LoadSteps()
00327 {
00328         Query q(m_db);
00329         q.get_result("select * from steps where player=" + Utility::l2string(GetNum()));
00330         while (q.fetch_row())
00331         {
00332                 db::Steps st(&m_db, &q);
00333                 Area area(m_db, st.area);
00334                 Steps s(m_db, *this, area, st.zcoord, st.ycoord);
00335         }
00336         q.free_result();
00337 }
00338 
00339 
00340 void Player::SetRace(long x)
00341 {
00342         m_account -> race = x;
00343 }
00344 
00345 
00346 long Player::GetRace()
00347 {
00348         return m_account -> race;
00349 }
00350 
00351 
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