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"
26 #include "jthread/jmutex.h"
31 #include "clientobject.h"
33 #include "inventorymanager.h"
34 #include "localplayer.h"
36 #include "particles.h"
40 class IWritableTextureSource;
41 class IWritableShaderSource;
42 class IWritableItemDefManager;
43 class IWritableNodeDefManager;
44 //class IWritableCraftDefManager;
45 class ClientMediaDownloader;
46 struct MapDrawControl;
52 struct QueuedMeshUpdate
56 bool ack_block_to_server;
62 enum LocalClientState {
69 A thread-safe queue of mesh update tasks
79 peer_id=0 adds with nobody to send to
81 void addBlock(v3s16 p, MeshMakeData *data,
82 bool ack_block_to_server, bool urgent);
84 // Returned pointer must be deleted
85 // Returns NULL if queue is empty
86 QueuedMeshUpdate * pop();
90 JMutexAutoLock lock(m_mutex);
91 return m_queue.size();
95 std::vector<QueuedMeshUpdate*> m_queue;
96 std::set<v3s16> m_urgents;
100 struct MeshUpdateResult
104 bool ack_block_to_server;
107 p(-1338,-1338,-1338),
109 ack_block_to_server(false)
114 class MeshUpdateThread : public JThread
118 MeshUpdateThread(IGameDef *gamedef):
125 MeshUpdateQueue m_queue_in;
127 MutexedQueue<MeshUpdateResult> m_queue_out;
131 v3s16 m_camera_offset;
138 CE_PLAYER_FORCE_MOVE,
142 CE_ADD_PARTICLESPAWNER,
143 CE_DELETE_PARTICLESPAWNER,
148 CE_OVERRIDE_DAY_NIGHT_RATIO,
153 ClientEventType type;
165 bool set_camera_point_target;
166 f32 camera_point_target_x;
167 f32 camera_point_target_y;
168 f32 camera_point_target_z;
171 std::string *formspec;
172 std::string *formname;
182 bool collisiondetection;
184 std::string *texture;
199 bool collisiondetection;
201 std::string *texture;
203 } add_particlespawner;
206 } delete_particlespawner;
235 video::SColor *bgcolor;
237 std::vector<std::string> *params;
242 } override_day_night_ratio;
257 void add(u16 command)
259 std::map<u16, u16>::iterator n = m_packets.find(command);
260 if(n == m_packets.end())
262 m_packets[command] = 1;
272 for(std::map<u16, u16>::iterator
273 i = m_packets.begin();
274 i != m_packets.end(); ++i)
280 void print(std::ostream &o)
282 for(std::map<u16, u16>::iterator
283 i = m_packets.begin();
284 i != m_packets.end(); ++i)
287 <<" count "<<i->second
294 std::map<u16, u16> m_packets;
297 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
301 NOTE: Nothing is thread-safe here.
305 IrrlichtDevice *device,
306 const char *playername,
307 std::string password,
308 MapDrawControl &control,
309 IWritableTextureSource *tsrc,
310 IWritableShaderSource *shsrc,
311 IWritableItemDefManager *itemdef,
312 IWritableNodeDefManager *nodedef,
313 ISoundManager *sound,
314 MtEventManager *event,
321 request all threads managed by client to be stopped
328 The name of the local player should already be set when
329 calling this, as it is sent in the initialization.
331 void connect(Address address);
334 Stuff that references the environment is valid only as
335 long as this is not called. (eg. Players)
336 If this throws a PeerNotFoundException, the connection has
339 void step(float dtime);
341 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
342 // Returns true if something was received
343 bool AsyncProcessPacket();
344 bool AsyncProcessData();
345 void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
347 void interact(u8 action, const PointedThing& pointed);
349 void sendNodemetaFields(v3s16 p, const std::string &formname,
350 const std::map<std::string, std::string> &fields);
351 void sendInventoryFields(const std::string &formname,
352 const std::map<std::string, std::string> &fields);
353 void sendInventoryAction(InventoryAction *a);
354 void sendChatMessage(const std::wstring &message);
355 void sendChangePassword(const std::wstring &oldpassword,
356 const std::wstring &newpassword);
357 void sendDamage(u8 damage);
358 void sendBreath(u16 breath);
362 ClientEnvironment& getEnv()
365 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
366 void removeNode(v3s16 p);
367 void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
369 void setPlayerControl(PlayerControl &control);
371 void selectPlayerItem(u16 item);
372 u16 getPlayerItem() const
373 { return m_playeritem; }
375 // Returns true if the inventory of the local player has been
376 // updated from the server. If it is true, it is set to false.
377 bool getLocalInventoryUpdated();
378 // Copies the inventory of the local player to parameter
379 void getLocalInventory(Inventory &dst);
381 /* InventoryManager interface */
382 Inventory* getInventory(const InventoryLocation &loc);
383 void inventoryAction(InventoryAction *a);
385 // Gets closest object pointed by the shootline
386 // Returns NULL if not found
387 ClientActiveObject * getSelectedActiveObject(
389 v3f from_pos_f_on_map,
390 core::line3d<f32> shootline_on_map
393 std::list<std::string> getConnectedPlayerNames();
395 float getAnimationTime();
398 void setCrack(int level, v3s16 pos);
400 void setHighlighted(v3s16 pos, bool show_hud);
401 v3s16 getHighlighted(){ return m_highlighted_pos; }
406 bool checkPrivilege(const std::string &priv)
407 { return (m_privileges.count(priv) != 0); }
409 bool getChatMessage(std::wstring &message);
410 void typeChatMessage(const std::wstring& message);
412 u64 getMapSeed(){ return m_map_seed; }
414 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
415 // Including blocks at appropriate edges
416 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
417 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
419 void updateCameraOffset(v3s16 camera_offset)
420 { m_mesh_update_thread.m_camera_offset = camera_offset; }
422 // Get event from queue. CE_NONE is returned if queue is empty.
423 ClientEvent getClientEvent();
426 { return m_access_denied; }
428 std::wstring accessDeniedReason()
429 { return m_access_denied_reason; }
431 bool itemdefReceived()
432 { return m_itemdef_received; }
433 bool nodedefReceived()
434 { return m_nodedef_received; }
436 { return m_media_downloader == NULL; }
438 float mediaReceiveProgress();
440 void afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font);
443 float getCurRate(void);
444 float getAvgRate(void);
446 // IGameDef interface
447 virtual IItemDefManager* getItemDefManager();
448 virtual INodeDefManager* getNodeDefManager();
449 virtual ICraftDefManager* getCraftDefManager();
450 virtual ITextureSource* getTextureSource();
451 virtual IShaderSource* getShaderSource();
452 virtual scene::ISceneManager* getSceneManager();
453 virtual u16 allocateUnknownNodeId(const std::string &name);
454 virtual ISoundManager* getSoundManager();
455 virtual MtEventManager* getEventManager();
456 virtual bool checkLocalPrivilege(const std::string &priv)
457 { return checkPrivilege(priv); }
458 virtual scene::IAnimatedMesh* getMesh(const std::string &filename);
460 // The following set of functions is used by ClientMediaDownloader
461 // Insert a media file appropriately into the appropriate manager
462 bool loadMedia(const std::string &data, const std::string &filename);
463 // Send a request for conventional media transfer
464 void request_media(const std::list<std::string> &file_requests);
465 // Send a notification that no conventional media transfer is needed
466 void received_media();
468 LocalClientState getState() { return m_state; }
470 void makeScreenshot(IrrlichtDevice *device);
474 // Virtual methods from con::PeerHandler
475 void peerAdded(con::Peer *peer);
476 void deletingPeer(con::Peer *peer, bool timeout);
481 void sendPlayerPos();
482 // Send the item number 'item' as player item to the server
483 void sendPlayerItem(u16 item);
485 float m_packetcounter_timer;
486 float m_connection_reinit_timer;
487 float m_avg_rtt_timer;
488 float m_playerpos_send_timer;
489 float m_ignore_damage_timer; // Used after server moves player
490 IntervalLimiter m_map_timer_and_unload_interval;
492 IWritableTextureSource *m_tsrc;
493 IWritableShaderSource *m_shsrc;
494 IWritableItemDefManager *m_itemdef;
495 IWritableNodeDefManager *m_nodedef;
496 ISoundManager *m_sound;
497 MtEventManager *m_event;
499 MeshUpdateThread m_mesh_update_thread;
500 ClientEnvironment m_env;
501 con::Connection m_con;
502 IrrlichtDevice *m_device;
503 // Server serialization version
506 bool m_inventory_updated;
507 Inventory *m_inventory_from_server;
508 float m_inventory_from_server_age;
509 std::set<v3s16> m_active_blocks;
510 PacketCounter m_packetcounter;
512 // Block mesh animation parameters
513 float m_animation_time;
516 v3s16 m_highlighted_pos;
517 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
519 //u32 m_daynight_ratio;
520 Queue<std::wstring> m_chat_queue;
521 // The seed returned by the server in TOCLIENT_INIT is stored here
523 std::string m_password;
524 bool m_access_denied;
525 std::wstring m_access_denied_reason;
526 Queue<ClientEvent> m_client_event_queue;
527 bool m_itemdef_received;
528 bool m_nodedef_received;
529 ClientMediaDownloader *m_media_downloader;
531 // time_of_day speed approximation for old protocol
532 bool m_time_of_day_set;
533 float m_last_time_of_day_f;
534 float m_time_of_day_update_timer;
536 // An interval for generally sending object positions and stuff
537 float m_recommended_send_interval;
540 float m_removed_sounds_check_timer;
541 // Mapping from server sound ids to our sound ids
542 std::map<s32, int> m_sounds_server_to_client;
543 // And the other way!
544 std::map<int, s32> m_sounds_client_to_server;
545 // And relations to objects
546 std::map<int, u16> m_sounds_to_objects;
549 std::set<std::string> m_privileges;
551 // Detached inventories
553 std::map<std::string, Inventory*> m_detached_inventories;
555 // Storage for mesh data for creating multiple instances of the same mesh
556 std::map<std::string, std::string> m_mesh_data;
559 LocalClientState m_state;
561 // Used for saving server map to disk client-side
566 #endif // !CLIENT_HEADER