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):
40 struct QueuedMeshUpdate
44 bool ack_block_to_server;
49 ack_block_to_server(false)
61 A thread-safe queue of mesh update tasks
73 JMutexAutoLock lock(m_mutex);
75 core::list<QueuedMeshUpdate*>::Iterator i;
76 for(i=m_queue.begin(); i!=m_queue.end(); i++)
78 QueuedMeshUpdate *q = *i;
84 peer_id=0 adds with nobody to send to
86 void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server)
88 DSTACK(__FUNCTION_NAME);
92 JMutexAutoLock lock(m_mutex);
95 Find if block is already in queue.
96 If it is, update the data and quit.
98 core::list<QueuedMeshUpdate*>::Iterator i;
99 for(i=m_queue.begin(); i!=m_queue.end(); i++)
101 QueuedMeshUpdate *q = *i;
107 if(ack_block_to_server)
108 q->ack_block_to_server = true;
116 QueuedMeshUpdate *q = new QueuedMeshUpdate;
119 q->ack_block_to_server = ack_block_to_server;
120 m_queue.push_back(q);
123 // Returned pointer must be deleted
124 // Returns NULL if queue is empty
125 QueuedMeshUpdate * pop()
127 JMutexAutoLock lock(m_mutex);
129 core::list<QueuedMeshUpdate*>::Iterator i = m_queue.begin();
130 if(i == m_queue.end())
132 QueuedMeshUpdate *q = *i;
139 JMutexAutoLock lock(m_mutex);
140 return m_queue.size();
144 core::list<QueuedMeshUpdate*> m_queue;
148 struct MeshUpdateResult
152 bool ack_block_to_server;
155 p(-1338,-1338,-1338),
157 ack_block_to_server(false)
162 class MeshUpdateThread : public SimpleThread
172 MeshUpdateQueue m_queue_in;
174 MutexedQueue<MeshUpdateResult> m_queue_out;
178 struct IncomingPacket
186 IncomingPacket(const IncomingPacket &a)
189 m_datalen = a.m_datalen;
190 m_refcount = a.m_refcount;
191 if(m_refcount != NULL)
194 IncomingPacket(u8 *data, u32 datalen)
196 m_data = new u8[datalen];
197 memcpy(m_data, data, datalen);
199 m_refcount = new s32(1);
203 if(m_refcount != NULL){
204 assert(*m_refcount > 0);
206 if(*m_refcount == 0){
213 /*IncomingPacket & operator=(IncomingPacket a)
216 m_datalen = a.m_datalen;
217 m_refcount = a.m_refcount;
227 class Client : public con::PeerHandler, public InventoryManager
231 NOTE: Nothing is thread-safe here.
235 IrrlichtDevice *device,
236 const char *playername,
237 MapDrawControl &control
242 The name of the local player should already be set when
243 calling this, as it is sent in the initialization.
245 void connect(Address address);
248 m_con.Connected() == true
249 AND m_server_ser_ver != SER_FMT_VER_INVALID
250 throws con::PeerNotFoundException if connection has been deleted,
253 bool connectedAndInitialized();
255 Stuff that references the environment is valid only as
256 long as this is not called. (eg. Players)
257 If this throws a PeerNotFoundException, the connection has
260 void step(float dtime);
262 // Called from updater thread
266 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
267 // Returns true if something was received
268 bool AsyncProcessPacket();
269 bool AsyncProcessData();
270 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
272 // Pops out a packet from the packet queue
273 //IncomingPacket getPacket();
275 void groundAction(u8 action, v3s16 nodepos_undersurface,
276 v3s16 nodepos_oversurface, u16 item);
277 void clickObject(u8 button, v3s16 blockpos, s16 id, u16 item);
278 void clickActiveObject(u8 button, u16 id, u16 item);
280 void sendSignText(v3s16 blockpos, s16 id, std::string text);
281 void sendSignNodeText(v3s16 p, std::string text);
282 void sendInventoryAction(InventoryAction *a);
283 void sendChatMessage(const std::wstring &message);
286 void removeNode(v3s16 p);
288 void addNode(v3s16 p, MapNode n);
290 void updateCamera(v3f pos, v3f dir);
292 // Returns InvalidPositionException if not found
293 MapNode getNode(v3s16 p);
295 NodeMetadata* getNodeMetadata(v3s16 p);
297 v3f getPlayerPosition();
299 void setPlayerControl(PlayerControl &control);
301 // Returns true if the inventory of the local player has been
302 // updated from the server. If it is true, it is set to false.
303 bool getLocalInventoryUpdated();
304 // Copies the inventory of the local player to parameter
305 void getLocalInventory(Inventory &dst);
307 InventoryContext *getInventoryContext();
309 Inventory* getInventory(InventoryContext *c, std::string id);
310 void inventoryAction(InventoryAction *a);
312 // Gets closest object pointed by the shootline
313 // Returns NULL if not found
314 MapBlockObject * getSelectedObject(
316 v3f from_pos_f_on_map,
317 core::line3d<f32> shootline_on_map
320 // Gets closest object pointed by the shootline
321 // Returns NULL if not found
322 ClientActiveObject * getSelectedActiveObject(
324 v3f from_pos_f_on_map,
325 core::line3d<f32> shootline_on_map
328 // Prints a line or two of info
329 void printDebugInfo(std::ostream &os);
331 u32 getDayNightRatio();
333 //void updateSomeExpiredMeshes();
335 void setTempMod(v3s16 p, NodeMod mod)
337 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
338 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
340 core::map<v3s16, MapBlock*> affected_blocks;
341 ((ClientMap&)m_env.getMap()).setTempMod(p, mod,
344 for(core::map<v3s16, MapBlock*>::Iterator
345 i = affected_blocks.getIterator();
346 i.atEnd() == false; i++)
348 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
351 void clearTempMod(v3s16 p)
353 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
354 assert(m_env.getMap().mapType() == MAPTYPE_CLIENT);
356 core::map<v3s16, MapBlock*> affected_blocks;
357 ((ClientMap&)m_env.getMap()).clearTempMod(p,
360 for(core::map<v3s16, MapBlock*>::Iterator
361 i = affected_blocks.getIterator();
362 i.atEnd() == false; i++)
364 i.getNode()->getValue()->updateMesh(m_env.getDayNightRatio());
370 //JMutexAutoLock lock(m_con_mutex); //bulk comment-out
371 con::Peer *peer = m_con.GetPeerNoEx(PEER_ID_SERVER);
374 return peer->avg_rtt;
377 bool getChatMessage(std::wstring &message)
379 if(m_chat_queue.size() == 0)
381 message = m_chat_queue.pop_front();
385 void addChatMessage(const std::wstring &message)
387 //JMutexAutoLock envlock(m_env_mutex); //bulk comment-out
388 LocalPlayer *player = m_env.getLocalPlayer();
389 assert(player != NULL);
390 std::wstring name = narrow_to_wide(player->getName());
391 m_chat_queue.push_back(
392 (std::wstring)L"<"+name+L"> "+message);
395 u64 getMapSeed(){ return m_map_seed; }
398 These are not thread-safe
400 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
401 // Including blocks at appropriate edges
402 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
406 // Virtual methods from con::PeerHandler
407 void peerAdded(con::Peer *peer);
408 void deletingPeer(con::Peer *peer, bool timeout);
413 void sendPlayerPos();
414 // This sends the player's current name etc to the server
415 void sendPlayerInfo();
417 float m_packetcounter_timer;
418 float m_delete_unused_sectors_timer;
419 float m_connection_reinit_timer;
420 float m_avg_rtt_timer;
421 float m_playerpos_send_timer;
423 MeshUpdateThread m_mesh_update_thread;
425 ClientEnvironment m_env;
427 con::Connection m_con;
429 IrrlichtDevice *m_device;
432 v3f camera_direction;
434 // Server serialization version
437 // This is behind m_env_mutex.
438 bool m_inventory_updated;
440 core::map<v3s16, bool> m_active_blocks;
442 PacketCounter m_packetcounter;
444 // Received from the server. 0-23999
447 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
449 //u32 m_daynight_ratio;
451 Queue<std::wstring> m_chat_queue;
453 // The seed returned by the server in TOCLIENT_INIT is stored here
456 InventoryContext m_inventory_context;
461 #endif // !CLIENT_HEADER