Logo
~Sockets~
~Examples~
~Contact~

Chunkinfo.cpp

Go to the documentation of this file.
00001 // Chunkinfo.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 
00022 #include "Chunkinfo.h"
00023 #include "Chunk.h"
00024 #include "InSocket.h"
00025 #include "Player.h"
00026 #include "Portal.h"
00027 #include "RoomDescription.h"
00028 
00029 
00030 // statics
00031 chunkinfos_t Chunkinfo::m_cache;
00032 
00033 
00034 Chunkinfo::Chunkinfo(Database& db,Chunk& chunk) : Object(db), m_chunk(chunk)
00035 {
00036         m_chunkinfo = Chunkinfo::GetChunkinfo(db, chunk);
00037 }
00038 
00039 
00040 Chunkinfo::Chunkinfo(Database& db,Chunk& chunk,int x,int y,infotype_t type) : Object(db), m_chunk(chunk)
00041 {
00042         m_chunkinfo = Chunkinfo::GetChunkinfo(db, chunk, x, y, type);
00043 }
00044 
00045 
00046 Chunkinfo::~Chunkinfo()
00047 {
00048 }
00049 
00050 
00051 void Chunkinfo::Save()
00052 {
00053         m_chunkinfo -> save();
00054 }
00055 
00056 
00057 bool Chunkinfo::Exists()
00058 {
00059         if (m_chunkinfo && m_chunkinfo -> num)
00060                 return true;
00061         return false;
00062 }
00063 
00064 
00065 long Chunkinfo::GetNum()
00066 {
00067         return m_chunkinfo -> num;
00068 }
00069 
00070 
00071 db::Chunkinfo *Chunkinfo::GetChunkinfo(Database& db,Chunk& chunk)
00072 {
00073         std::list<db::Chunkinfo *>& ref = m_cache[chunk.GetNum()];
00074         db::Chunkinfo *p = new db::Chunkinfo(&db);
00075         p -> chunk = chunk.GetNum();
00076         ref.push_back(p);
00077         return p;
00078 }
00079 
00080 
00081 db::Chunkinfo *Chunkinfo::GetChunkinfo(Database& db,Chunk& chunk,long num)
00082 {
00083         std::list<db::Chunkinfo *>& ref = m_cache[chunk.GetNum()];
00084         for (std::list<db::Chunkinfo *>::iterator it = ref.begin(); it != ref.end(); it++)
00085         {
00086                 db::Chunkinfo *p = *it;
00087                 if (p -> num == num)
00088                         return p;
00089         }
00090         db::Chunkinfo *p = new db::Chunkinfo(db, num);
00091         p -> chunk = chunk.GetNum();
00092         ref.push_back(p);
00093         return p;
00094 }
00095 
00096 
00097 void Chunkinfo::SetType(infotype_t x)
00098 {
00099         m_chunkinfo -> type = x;
00100 }
00101 
00102 
00103 void Chunkinfo::SetData(const std::string& x)
00104 {
00105         m_chunkinfo -> data = x;
00106 }
00107 
00108 
00109 Chunkinfo::infotype_t Chunkinfo::GetType()
00110 {
00111         return (infotype_t)m_chunkinfo -> type;
00112 }
00113 
00114 
00115 const std::string& Chunkinfo::GetData()
00116 {
00117         return m_chunkinfo -> data;
00118 }
00119 
00120 
00121 void Chunkinfo::SetX(int x)
00122 {
00123         m_chunkinfo -> x = x;
00124 }
00125 
00126 
00127 void Chunkinfo::SetY(int x)
00128 {
00129         m_chunkinfo -> y = x;
00130 }
00131 
00132 
00133 int Chunkinfo::GetX()
00134 {
00135         return m_chunkinfo -> x;
00136 }
00137 
00138 
00139 int Chunkinfo::GetY()
00140 {
00141         return m_chunkinfo -> y;
00142 }
00143 
00144 
00145 db::Chunkinfo *Chunkinfo::GetChunkinfo(Database& db,Chunk& chunk,int x,int y,infotype_t type)
00146 {
00147         std::list<db::Chunkinfo *>& ref = m_cache[chunk.GetNum()];
00148         for (std::list<db::Chunkinfo *>::iterator it = ref.begin(); it != ref.end(); it++)
00149         {
00150                 db::Chunkinfo *p = *it;
00151                 if (p -> x == x && p -> y == y && p -> type == type)
00152                 {
00153                         return p;
00154                 }
00155         }
00156         db::Chunkinfo *p = new db::Chunkinfo(db, chunk.GetNum(), type, x, y);
00157         p -> chunk = chunk.GetNum();
00158         p -> type = type;
00159         p -> x = x;
00160         p -> y = y;
00161         ref.push_back(p);
00162         return p;
00163 }
00164 
00165 
00166 std::list<db::Chunkinfo *>& Chunkinfo::GetChunkinfo(Chunk& chunk)
00167 {
00168         return m_cache[chunk.GetNum()];
00169 }
00170 
00171 
00172 Chunkinfo *Chunkinfo::GetInstance(Database& db,Chunk& chunk,int x,int y,infotype_t type)
00173 {
00174         switch (type)
00175         {
00176         case TYPE_PORTAL:
00177                 return new Portal(db, chunk, x, y);
00178         case TYPE_ROOM_DESCRIPTION:
00179                 return new RoomDescription(db, chunk, x, y);
00180         }
00181         return NULL;
00182 }
00183 
00184 
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