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,
162 CE_ADD_PARTICLESPAWNER,
163 CE_DELETE_PARTICLESPAWNER,
171 ClientEventType type;
183 bool set_camera_point_target;
184 f32 camera_point_target_x;
185 f32 camera_point_target_y;
186 f32 camera_point_target_z;
189 std::string *formspec;
190 std::string *formname;
200 bool collisiondetection;
201 std::string *texture;
216 bool collisiondetection;
217 std::string *texture;
219 } add_particlespawner;
222 } delete_particlespawner;
249 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
253 NOTE: Nothing is thread-safe here.
257 IrrlichtDevice *device,
258 const char *playername,
259 std::string password,
260 MapDrawControl &control,
261 IWritableTextureSource *tsrc,
262 IWritableShaderSource *shsrc,
263 IWritableItemDefManager *itemdef,
264 IWritableNodeDefManager *nodedef,
265 ISoundManager *sound,
266 MtEventManager *event,
272 The name of the local player should already be set when
273 calling this, as it is sent in the initialization.
275 void connect(Address address);
278 m_con.Connected() == true
279 AND m_server_ser_ver != SER_FMT_VER_INVALID
280 throws con::PeerNotFoundException if connection has been deleted,
283 bool connectedAndInitialized();
285 Stuff that references the environment is valid only as
286 long as this is not called. (eg. Players)
287 If this throws a PeerNotFoundException, the connection has
290 void step(float dtime);
292 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
293 // Returns true if something was received
294 bool AsyncProcessPacket();
295 bool AsyncProcessData();
296 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
298 void interact(u8 action, const PointedThing& pointed);
300 void sendNodemetaFields(v3s16 p, const std::string &formname,
301 const std::map<std::string, std::string> &fields);
302 void sendInventoryFields(const std::string &formname,
303 const std::map<std::string, std::string> &fields);
304 void sendInventoryAction(InventoryAction *a);
305 void sendChatMessage(const std::wstring &message);
306 void sendChangePassword(const std::wstring oldpassword,
307 const std::wstring newpassword);
308 void sendDamage(u8 damage);
309 void sendBreath(u16 breath);
312 ClientEnvironment& getEnv()
315 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
316 void removeNode(v3s16 p);
317 void addNode(v3s16 p, MapNode n);
319 void setPlayerControl(PlayerControl &control);
321 void selectPlayerItem(u16 item);
322 u16 getPlayerItem() const
323 { return m_playeritem; }
325 // Returns true if the inventory of the local player has been
326 // updated from the server. If it is true, it is set to false.
327 bool getLocalInventoryUpdated();
328 // Copies the inventory of the local player to parameter
329 void getLocalInventory(Inventory &dst);
331 /* InventoryManager interface */
332 Inventory* getInventory(const InventoryLocation &loc);
333 void inventoryAction(InventoryAction *a);
335 // Gets closest object pointed by the shootline
336 // Returns NULL if not found
337 ClientActiveObject * getSelectedActiveObject(
339 v3f from_pos_f_on_map,
340 core::line3d<f32> shootline_on_map
343 // Prints a line or two of info
344 void printDebugInfo(std::ostream &os);
346 std::list<std::string> getConnectedPlayerNames();
348 float getAnimationTime();
351 void setCrack(int level, v3s16 pos);
356 bool checkPrivilege(const std::string &priv)
357 { return (m_privileges.count(priv) != 0); }
359 bool getChatMessage(std::wstring &message);
360 void typeChatMessage(const std::wstring& message);
362 u64 getMapSeed(){ return m_map_seed; }
364 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
365 // Including blocks at appropriate edges
366 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
367 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
369 // Get event from queue. CE_NONE is returned if queue is empty.
370 ClientEvent getClientEvent();
373 { return m_access_denied; }
375 std::wstring accessDeniedReason()
376 { return m_access_denied_reason; }
378 float mediaReceiveProgress()
380 if (!m_media_receive_started) return 0;
381 return 1.0 * m_media_received_count / m_media_count;
384 bool texturesReceived()
385 { return m_media_receive_started && m_media_received_count == m_media_count; }
386 bool itemdefReceived()
387 { return m_itemdef_received; }
388 bool nodedefReceived()
389 { return m_nodedef_received; }
391 void afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font);
395 // IGameDef interface
396 virtual IItemDefManager* getItemDefManager();
397 virtual INodeDefManager* getNodeDefManager();
398 virtual ICraftDefManager* getCraftDefManager();
399 virtual ITextureSource* getTextureSource();
400 virtual IShaderSource* getShaderSource();
401 virtual u16 allocateUnknownNodeId(const std::string &name);
402 virtual ISoundManager* getSoundManager();
403 virtual MtEventManager* getEventManager();
404 virtual bool checkLocalPrivilege(const std::string &priv)
405 { return checkPrivilege(priv); }
409 // Insert a media file appropriately into the appropriate manager
410 bool loadMedia(const std::string &data, const std::string &filename);
412 void request_media(const std::list<MediaRequest> &file_requests);
414 // Virtual methods from con::PeerHandler
415 void peerAdded(con::Peer *peer);
416 void deletingPeer(con::Peer *peer, bool timeout);
421 void sendPlayerPos();
422 // This sends the player's current name etc to the server
423 void sendPlayerInfo();
424 // Send the item number 'item' as player item to the server
425 void sendPlayerItem(u16 item);
427 float m_packetcounter_timer;
428 float m_connection_reinit_timer;
429 float m_avg_rtt_timer;
430 float m_playerpos_send_timer;
431 float m_ignore_damage_timer; // Used after server moves player
432 IntervalLimiter m_map_timer_and_unload_interval;
434 IWritableTextureSource *m_tsrc;
435 IWritableShaderSource *m_shsrc;
436 IWritableItemDefManager *m_itemdef;
437 IWritableNodeDefManager *m_nodedef;
438 ISoundManager *m_sound;
439 MtEventManager *m_event;
441 MeshUpdateThread m_mesh_update_thread;
442 std::list<MediaFetchThread*> m_media_fetch_threads;
443 ClientEnvironment m_env;
444 con::Connection m_con;
445 IrrlichtDevice *m_device;
446 // Server serialization version
449 bool m_inventory_updated;
450 Inventory *m_inventory_from_server;
451 float m_inventory_from_server_age;
452 std::set<v3s16> m_active_blocks;
453 PacketCounter m_packetcounter;
454 // Block mesh animation parameters
455 float m_animation_time;
458 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
460 //u32 m_daynight_ratio;
461 Queue<std::wstring> m_chat_queue;
462 // The seed returned by the server in TOCLIENT_INIT is stored here
464 std::string m_password;
465 bool m_access_denied;
466 std::wstring m_access_denied_reason;
467 Queue<ClientEvent> m_client_event_queue;
468 FileCache m_media_cache;
469 // Mapping from media file name to SHA1 checksum
470 std::map<std::string, std::string> m_media_name_sha1_map;
471 bool m_media_receive_started;
473 u32 m_media_received_count;
474 bool m_itemdef_received;
475 bool m_nodedef_received;
476 friend class FarMesh;
478 // time_of_day speed approximation for old protocol
479 bool m_time_of_day_set;
480 float m_last_time_of_day_f;
481 float m_time_of_day_update_timer;
483 // An interval for generally sending object positions and stuff
484 float m_recommended_send_interval;
487 float m_removed_sounds_check_timer;
488 // Mapping from server sound ids to our sound ids
489 std::map<s32, int> m_sounds_server_to_client;
490 // And the other way!
491 std::map<int, s32> m_sounds_client_to_server;
492 // And relations to objects
493 std::map<int, u16> m_sounds_to_objects;
496 std::set<std::string> m_privileges;
498 // Detached inventories
500 std::map<std::string, Inventory*> m_detached_inventories;
503 #endif // !CLIENT_HEADER