3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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.
23 #include "connection.h"
24 #include "environment.h"
25 #include "irrlichttypes_extrabloated.h"
30 #include "clientobject.h"
32 #include "inventorymanager.h"
34 #include "filecache.h"
35 #include "localplayer.h"
37 #include "particles.h"
38 #include "util/pointedthing.h"
44 class IWritableTextureSource;
45 class IWritableShaderSource;
46 class IWritableItemDefManager;
47 class IWritableNodeDefManager;
48 //class IWritableCraftDefManager;
49 class ClientEnvironment;
50 struct MapDrawControl;
53 class ClientNotReadyException : public BaseException
56 ClientNotReadyException(const char *s):
61 struct QueuedMeshUpdate
65 bool ack_block_to_server;
72 A thread-safe queue of mesh update tasks
82 peer_id=0 adds with nobody to send to
84 void addBlock(v3s16 p, MeshMakeData *data,
85 bool ack_block_to_server, bool urgent);
87 // Returned pointer must be deleted
88 // Returns NULL if queue is empty
89 QueuedMeshUpdate * pop();
93 JMutexAutoLock lock(m_mutex);
94 return m_queue.size();
98 std::vector<QueuedMeshUpdate*> m_queue;
99 std::set<v3s16> m_urgents;
103 struct MeshUpdateResult
107 bool ack_block_to_server;
110 p(-1338,-1338,-1338),
112 ack_block_to_server(false)
117 class MeshUpdateThread : public SimpleThread
121 MeshUpdateThread(IGameDef *gamedef):
128 MeshUpdateQueue m_queue_in;
130 MutexedQueue<MeshUpdateResult> m_queue_out;
135 class MediaFetchThread : public SimpleThread
139 MediaFetchThread(IGameDef *gamedef):
146 std::list<MediaRequest> m_file_requests;
147 MutexedQueue<std::pair<std::string, std::string> > m_file_data;
148 std::list<MediaRequest> m_failed;
149 std::string m_remote_url;
157 CE_PLAYER_FORCE_MOVE,
165 ClientEventType type;
177 bool set_camera_point_target;
178 f32 camera_point_target_x;
179 f32 camera_point_target_y;
180 f32 camera_point_target_z;
183 std::string* formspec;
184 std::string* formname;
191 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
195 NOTE: Nothing is thread-safe here.
199 IrrlichtDevice *device,
200 const char *playername,
201 std::string password,
202 MapDrawControl &control,
203 IWritableTextureSource *tsrc,
204 IWritableShaderSource *shsrc,
205 IWritableItemDefManager *itemdef,
206 IWritableNodeDefManager *nodedef,
207 ISoundManager *sound,
208 MtEventManager *event
213 The name of the local player should already be set when
214 calling this, as it is sent in the initialization.
216 void connect(Address address);
219 m_con.Connected() == true
220 AND m_server_ser_ver != SER_FMT_VER_INVALID
221 throws con::PeerNotFoundException if connection has been deleted,
224 bool connectedAndInitialized();
226 Stuff that references the environment is valid only as
227 long as this is not called. (eg. Players)
228 If this throws a PeerNotFoundException, the connection has
231 void step(float dtime);
233 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
234 // Returns true if something was received
235 bool AsyncProcessPacket();
236 bool AsyncProcessData();
237 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
239 void interact(u8 action, const PointedThing& pointed);
241 void sendNodemetaFields(v3s16 p, const std::string &formname,
242 const std::map<std::string, std::string> &fields);
243 void sendInventoryFields(const std::string &formname,
244 const std::map<std::string, std::string> &fields);
245 void sendInventoryAction(InventoryAction *a);
246 void sendChatMessage(const std::wstring &message);
247 void sendChangePassword(const std::wstring oldpassword,
248 const std::wstring newpassword);
249 void sendDamage(u8 damage);
252 ClientEnvironment& getEnv()
255 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
256 void removeNode(v3s16 p);
257 void addNode(v3s16 p, MapNode n);
259 void setPlayerControl(PlayerControl &control);
261 void selectPlayerItem(u16 item);
262 u16 getPlayerItem() const
263 { return m_playeritem; }
265 // Returns true if the inventory of the local player has been
266 // updated from the server. If it is true, it is set to false.
267 bool getLocalInventoryUpdated();
268 // Copies the inventory of the local player to parameter
269 void getLocalInventory(Inventory &dst);
271 /* InventoryManager interface */
272 Inventory* getInventory(const InventoryLocation &loc);
273 void inventoryAction(InventoryAction *a);
275 // Gets closest object pointed by the shootline
276 // Returns NULL if not found
277 ClientActiveObject * getSelectedActiveObject(
279 v3f from_pos_f_on_map,
280 core::line3d<f32> shootline_on_map
283 // Prints a line or two of info
284 void printDebugInfo(std::ostream &os);
286 std::list<std::wstring> getConnectedPlayerNames();
288 float getAnimationTime();
291 void setCrack(int level, v3s16 pos);
295 bool checkPrivilege(const std::string &priv)
296 { return (m_privileges.count(priv) != 0); }
298 bool getChatMessage(std::wstring &message);
299 void typeChatMessage(const std::wstring& message);
301 u64 getMapSeed(){ return m_map_seed; }
303 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
304 // Including blocks at appropriate edges
305 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
306 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
308 // Get event from queue. CE_NONE is returned if queue is empty.
309 ClientEvent getClientEvent();
312 { return m_access_denied; }
314 std::wstring accessDeniedReason()
315 { return m_access_denied_reason; }
317 float mediaReceiveProgress()
319 if (!m_media_receive_started) return 0;
320 return 1.0 * m_media_received_count / m_media_count;
323 bool texturesReceived()
324 { return m_media_receive_started && m_media_received_count == m_media_count; }
325 bool itemdefReceived()
326 { return m_itemdef_received; }
327 bool nodedefReceived()
328 { return m_nodedef_received; }
330 void afterContentReceived();
334 // IGameDef interface
335 virtual IItemDefManager* getItemDefManager();
336 virtual INodeDefManager* getNodeDefManager();
337 virtual ICraftDefManager* getCraftDefManager();
338 virtual ITextureSource* getTextureSource();
339 virtual IShaderSource* getShaderSource();
340 virtual u16 allocateUnknownNodeId(const std::string &name);
341 virtual ISoundManager* getSoundManager();
342 virtual MtEventManager* getEventManager();
343 virtual bool checkLocalPrivilege(const std::string &priv)
344 { return checkPrivilege(priv); }
348 // Insert a media file appropriately into the appropriate manager
349 bool loadMedia(const std::string &data, const std::string &filename);
351 void request_media(const std::list<MediaRequest> &file_requests);
353 // Virtual methods from con::PeerHandler
354 void peerAdded(con::Peer *peer);
355 void deletingPeer(con::Peer *peer, bool timeout);
360 void sendPlayerPos();
361 // This sends the player's current name etc to the server
362 void sendPlayerInfo();
363 // Send the item number 'item' as player item to the server
364 void sendPlayerItem(u16 item);
366 float m_packetcounter_timer;
367 float m_connection_reinit_timer;
368 float m_avg_rtt_timer;
369 float m_playerpos_send_timer;
370 float m_ignore_damage_timer; // Used after server moves player
371 IntervalLimiter m_map_timer_and_unload_interval;
373 IWritableTextureSource *m_tsrc;
374 IWritableShaderSource *m_shsrc;
375 IWritableItemDefManager *m_itemdef;
376 IWritableNodeDefManager *m_nodedef;
377 ISoundManager *m_sound;
378 MtEventManager *m_event;
380 MeshUpdateThread m_mesh_update_thread;
381 std::list<MediaFetchThread*> m_media_fetch_threads;
382 ClientEnvironment m_env;
383 con::Connection m_con;
384 IrrlichtDevice *m_device;
385 // Server serialization version
388 bool m_inventory_updated;
389 Inventory *m_inventory_from_server;
390 float m_inventory_from_server_age;
391 std::set<v3s16> m_active_blocks;
392 PacketCounter m_packetcounter;
393 // Block mesh animation parameters
394 float m_animation_time;
397 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
399 //u32 m_daynight_ratio;
400 Queue<std::wstring> m_chat_queue;
401 // The seed returned by the server in TOCLIENT_INIT is stored here
403 std::string m_password;
404 bool m_access_denied;
405 std::wstring m_access_denied_reason;
406 Queue<ClientEvent> m_client_event_queue;
407 FileCache m_media_cache;
408 // Mapping from media file name to SHA1 checksum
409 std::map<std::string, std::string> m_media_name_sha1_map;
410 bool m_media_receive_started;
412 u32 m_media_received_count;
413 bool m_itemdef_received;
414 bool m_nodedef_received;
415 friend class FarMesh;
417 // time_of_day speed approximation for old protocol
418 bool m_time_of_day_set;
419 float m_last_time_of_day_f;
420 float m_time_of_day_update_timer;
422 // An interval for generally sending object positions and stuff
423 float m_recommended_send_interval;
426 float m_removed_sounds_check_timer;
427 // Mapping from server sound ids to our sound ids
428 std::map<s32, int> m_sounds_server_to_client;
429 // And the other way!
430 std::map<int, s32> m_sounds_client_to_server;
431 // And relations to objects
432 std::map<int, u16> m_sounds_to_objects;
435 std::set<std::string> m_privileges;
437 // Detached inventories
439 std::map<std::string, Inventory*> m_detached_inventories;
442 #endif // !CLIENT_HEADER