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 v3f getPlayerPosition();
172 void setPlayerControl(PlayerControl &control);
174 // Returns true if the inventory of the local player has been
175 // updated from the server. If it is true, it is set to false.
176 bool getLocalInventoryUpdated();
177 // Copies the inventory of the local player to parameter
178 void getLocalInventory(Inventory &dst);
180 // Gets closest object pointed by the shootline
181 // Returns NULL if not found
182 MapBlockObject * getSelectedObject(
184 v3f from_pos_f_on_map,
185 core::line3d<f32> shootline_on_map
188 // Prints a line or two of info
189 void printDebugInfo(std::ostream &os);
191 u32 getDayNightRatio();
193 //void updateSomeExpiredMeshes();
195 void setTempMod(v3s16 p, NodeMod mod)
197 JMutexAutoLock envlock(m_env_mutex);
198 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
200 core::map<v3s16, MapBlock*> affected_blocks;
201 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
204 for(core::map<v3s16, MapBlock*>::Iterator
205 i = affected_blocks.getIterator();
206 i.atEnd() == false; i++)
208 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
211 void clearTempMod(v3s16 p)
213 JMutexAutoLock envlock(m_env_mutex);
214 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
216 core::map<v3s16, MapBlock*> affected_blocks;
217 ((ClientMap&)m_env.getMap()).clearTempMod(p,
220 for(core::map<v3s16, MapBlock*>::Iterator
221 i = affected_blocks.getIterator();
222 i.atEnd() == false; i++)
224 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
230 JMutexAutoLock lock(m_con_mutex);
231 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
234 return peer->avg_rtt;
237 bool getChatMessage(std::wstring &message)
239 if(m_chat_queue.size() == 0)
241 message = m_chat_queue.pop_front();
245 void addChatMessage(const std::wstring &message)
247 JMutexAutoLock envlock(m_env_mutex);
248 LocalPlayer *player = m_env.getLocalPlayer();
249 assert(player != NULL);
250 std::wstring name = narrow_to_wide(player->getName());
251 m_chat_queue.push_back(
252 (std::wstring)L"<"+name+L"> "+message);
257 // Virtual methods from con::PeerHandler
258 void peerAdded(con::Peer *peer);
259 void deletingPeer(con::Peer *peer, bool timeout);
264 void sendPlayerPos();
265 // This sends the player's current name etc to the server
266 void sendPlayerInfo();
268 float m_packetcounter_timer;
269 float m_delete_unused_sectors_timer;
270 float m_connection_reinit_timer;
271 float m_avg_rtt_timer;
272 float m_playerpos_send_timer;
274 ClientUpdateThread m_thread;
276 // NOTE: If connection and environment are both to be locked,
277 // environment shall be locked first.
279 ClientEnvironment m_env;
282 con::Connection m_con;
285 core::list<IncomingPacket> m_incoming_queue;
286 JMutex m_incoming_queue_mutex;
288 IrrlichtDevice *m_device;
291 v3f camera_direction;
293 // Server serialization version
297 JMutex m_step_dtime_mutex;
299 // This is behind m_env_mutex.
300 bool m_inventory_updated;
302 core::map<v3s16, bool> m_active_blocks;
304 PacketCounter m_packetcounter;
306 // Received from the server. 0-23999
307 MutexedVariable<u32> m_time_of_day;
309 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
311 //u32 m_daynight_ratio;
313 Queue<std::wstring> m_chat_queue;
318 #endif // !CLIENT_HEADER