3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "connection.h"
26 #include "environment.h"
27 #include "common_irrlicht.h"
30 #include "clientobject.h"
32 class ClientNotReadyException : public BaseException
35 ClientNotReadyException(const char *s):
42 class ClientUpdateThread : public SimpleThread
48 ClientUpdateThread(Client *client):
65 IncomingPacket(const IncomingPacket &a)
68 m_datalen = a.m_datalen;
69 m_refcount = a.m_refcount;
70 if(m_refcount != NULL)
73 IncomingPacket(u8 *data, u32 datalen)
75 m_data = new u8[datalen];
76 memcpy(m_data, data, datalen);
78 m_refcount = new s32(1);
82 if(m_refcount != NULL){
83 assert(*m_refcount > 0);
92 /*IncomingPacket & operator=(IncomingPacket a)
95 m_datalen = a.m_datalen;
96 m_refcount = a.m_refcount;
105 class Client : public con::PeerHandler
109 NOTE: Every public method should be thread-safe
112 IrrlichtDevice *device,
113 const char *playername,
114 MapDrawControl &control
119 The name of the local player should already be set when
120 calling this, as it is sent in the initialization.
122 void connect(Address address);
125 m_con.Connected() == true
126 AND m_server_ser_ver != SER_FMT_VER_INVALID
127 throws con::PeerNotFoundException if connection has been deleted,
130 bool connectedAndInitialized();
132 Stuff that references the environment is valid only as
133 long as this is not called. (eg. Players)
134 If this throws a PeerNotFoundException, the connection has
137 void step(float dtime);
139 // Called from updater thread
143 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
144 // Returns true if something was received
145 bool AsyncProcessPacket();
146 bool AsyncProcessData();
147 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
149 // Pops out a packet from the packet queue
150 IncomingPacket getPacket();
152 void groundAction(u8 action, v3s16 nodepos_undersurface,
153 v3s16 nodepos_oversurface, u16 item);
154 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
156 void sendSignText(v3s16 blockpos, s16 id, std::string text);
157 void sendInventoryAction(InventoryAction *a);
158 void sendChatMessage(const std::wstring &message);
161 void removeNode(v3s16 p);
163 void addNode(v3s16 p, MapNode n);
165 void updateCamera(v3f pos, v3f dir);
167 // Returns InvalidPositionException if not found
168 MapNode getNode(v3s16 p);
170 NodeMetadata* getNodeMetadataClone(v3s16 p);
172 v3f getPlayerPosition();
174 void setPlayerControl(PlayerControl &control);
176 // Returns true if the inventory of the local player has been
177 // updated from the server. If it is true, it is set to false.
178 bool getLocalInventoryUpdated();
179 // Copies the inventory of the local player to parameter
180 void getLocalInventory(Inventory &dst);
182 // Gets closest object pointed by the shootline
183 // Returns NULL if not found
184 MapBlockObject * getSelectedObject(
186 v3f from_pos_f_on_map,
187 core::line3d<f32> shootline_on_map
190 // Prints a line or two of info
191 void printDebugInfo(std::ostream &os);
193 u32 getDayNightRatio();
195 //void updateSomeExpiredMeshes();
197 void setTempMod(v3s16 p, NodeMod mod)
199 JMutexAutoLock envlock(m_env_mutex);
200 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
202 core::map<v3s16, MapBlock*> affected_blocks;
203 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
206 for(core::map<v3s16, MapBlock*>::Iterator
207 i = affected_blocks.getIterator();
208 i.atEnd() == false; i++)
210 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
213 void clearTempMod(v3s16 p)
215 JMutexAutoLock envlock(m_env_mutex);
216 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
218 core::map<v3s16, MapBlock*> affected_blocks;
219 ((ClientMap&)m_env.getMap()).clearTempMod(p,
222 for(core::map<v3s16, MapBlock*>::Iterator
223 i = affected_blocks.getIterator();
224 i.atEnd() == false; i++)
226 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
232 JMutexAutoLock lock(m_con_mutex);
233 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
236 return peer->avg_rtt;
239 bool getChatMessage(std::wstring &message)
241 if(m_chat_queue.size() == 0)
243 message = m_chat_queue.pop_front();
247 void addChatMessage(const std::wstring &message)
249 JMutexAutoLock envlock(m_env_mutex);
250 LocalPlayer *player = m_env.getLocalPlayer();
251 assert(player != NULL);
252 std::wstring name = narrow_to_wide(player->getName());
253 m_chat_queue.push_back(
254 (std::wstring)L"<"+name+L"> "+message);
257 u64 getMapSeed(){ return m_map_seed; }
261 // Virtual methods from con::PeerHandler
262 void peerAdded(con::Peer *peer);
263 void deletingPeer(con::Peer *peer, bool timeout);
268 void sendPlayerPos();
269 // This sends the player's current name etc to the server
270 void sendPlayerInfo();
272 float m_packetcounter_timer;
273 float m_delete_unused_sectors_timer;
274 float m_connection_reinit_timer;
275 float m_avg_rtt_timer;
276 float m_playerpos_send_timer;
278 ClientUpdateThread m_thread;
280 // NOTE: If connection and environment are both to be locked,
281 // environment shall be locked first.
283 ClientEnvironment m_env;
286 con::Connection m_con;
289 core::list<IncomingPacket> m_incoming_queue;
290 JMutex m_incoming_queue_mutex;
292 IrrlichtDevice *m_device;
295 v3f camera_direction;
297 // Server serialization version
301 JMutex m_step_dtime_mutex;
303 // This is behind m_env_mutex.
304 bool m_inventory_updated;
306 core::map<v3s16, bool> m_active_blocks;
308 PacketCounter m_packetcounter;
310 // Received from the server. 0-23999
311 MutexedVariable<u32> m_time_of_day;
313 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
315 //u32 m_daynight_ratio;
317 Queue<std::wstring> m_chat_queue;
319 // The seed returned by the server in TOCLIENT_INIT is stored here
325 #endif // !CLIENT_HEADER