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 "network/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"
37 #include "network/networkpacket.h"
41 class IWritableTextureSource;
42 class IWritableShaderSource;
43 class IWritableItemDefManager;
44 class IWritableNodeDefManager;
45 //class IWritableCraftDefManager;
46 class ClientMediaDownloader;
47 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;
175 //} textures_updated;
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
329 The name of the local player should already be set when
330 calling this, as it is sent in the initialization.
332 void connect(Address address,
333 const std::string &address_name,
334 bool is_local_server);
337 Stuff that references the environment is valid only as
338 long as this is not called. (eg. Players)
339 If this throws a PeerNotFoundException, the connection has
342 void step(float dtime);
348 void handleCommand(NetworkPacket* pkt);
350 void handleCommand_Null(NetworkPacket* pkt) {};
351 void handleCommand_Deprecated(NetworkPacket* pkt);
352 void handleCommand_Init(NetworkPacket* pkt);
353 void handleCommand_AccessDenied(NetworkPacket* pkt);
354 void handleCommand_RemoveNode(NetworkPacket* pkt);
355 void handleCommand_AddNode(NetworkPacket* pkt);
356 void handleCommand_BlockData(NetworkPacket* pkt);
357 void handleCommand_Inventory(NetworkPacket* pkt);
358 void handleCommand_TimeOfDay(NetworkPacket* pkt);
359 void handleCommand_ChatMessage(NetworkPacket* pkt);
360 void handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt);
361 void handleCommand_ActiveObjectMessages(NetworkPacket* pkt);
362 void handleCommand_Movement(NetworkPacket* pkt);
363 void handleCommand_HP(NetworkPacket* pkt);
364 void handleCommand_Breath(NetworkPacket* pkt);
365 void handleCommand_MovePlayer(NetworkPacket* pkt);
366 void handleCommand_PlayerItem(NetworkPacket* pkt);
367 void handleCommand_DeathScreen(NetworkPacket* pkt);
368 void handleCommand_AnnounceMedia(NetworkPacket* pkt);
369 void handleCommand_Media(NetworkPacket* pkt);
370 void handleCommand_ToolDef(NetworkPacket* pkt);
371 void handleCommand_NodeDef(NetworkPacket* pkt);
372 void handleCommand_CraftItemDef(NetworkPacket* pkt);
373 void handleCommand_ItemDef(NetworkPacket* pkt);
374 void handleCommand_PlaySound(NetworkPacket* pkt);
375 void handleCommand_StopSound(NetworkPacket* pkt);
376 void handleCommand_Privileges(NetworkPacket* pkt);
377 void handleCommand_InventoryFormSpec(NetworkPacket* pkt);
378 void handleCommand_DetachedInventory(NetworkPacket* pkt);
379 void handleCommand_ShowFormSpec(NetworkPacket* pkt);
380 void handleCommand_SpawnParticle(NetworkPacket* pkt);
381 void handleCommand_AddParticleSpawner(NetworkPacket* pkt);
382 void handleCommand_DeleteParticleSpawner(NetworkPacket* pkt);
383 void handleCommand_HudAdd(NetworkPacket* pkt);
384 void handleCommand_HudRemove(NetworkPacket* pkt);
385 void handleCommand_HudChange(NetworkPacket* pkt);
386 void handleCommand_HudSetFlags(NetworkPacket* pkt);
387 void handleCommand_HudSetParam(NetworkPacket* pkt);
388 void handleCommand_HudSetSky(NetworkPacket* pkt);
389 void handleCommand_OverrideDayNightRatio(NetworkPacket* pkt);
390 void handleCommand_LocalPlayerAnimations(NetworkPacket* pkt);
391 void handleCommand_EyeOffset(NetworkPacket* pkt);
393 void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
395 // Returns true if something was received
396 bool AsyncProcessPacket();
397 bool AsyncProcessData();
398 void Send(NetworkPacket* pkt);
400 void interact(u8 action, const PointedThing& pointed);
402 void sendNodemetaFields(v3s16 p, const std::string &formname,
403 const std::map<std::string, std::string> &fields);
404 void sendInventoryFields(const std::string &formname,
405 const std::map<std::string, std::string> &fields);
406 void sendInventoryAction(InventoryAction *a);
407 void sendChatMessage(const std::wstring &message);
408 void sendChangePassword(const std::wstring &oldpassword,
409 const std::wstring &newpassword);
410 void sendDamage(u8 damage);
411 void sendBreath(u16 breath);
415 ClientEnvironment& getEnv()
418 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
419 void removeNode(v3s16 p);
420 void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
422 void setPlayerControl(PlayerControl &control);
424 void selectPlayerItem(u16 item);
425 u16 getPlayerItem() const
426 { return m_playeritem; }
428 // Returns true if the inventory of the local player has been
429 // updated from the server. If it is true, it is set to false.
430 bool getLocalInventoryUpdated();
431 // Copies the inventory of the local player to parameter
432 void getLocalInventory(Inventory &dst);
434 /* InventoryManager interface */
435 Inventory* getInventory(const InventoryLocation &loc);
436 void inventoryAction(InventoryAction *a);
438 // Gets closest object pointed by the shootline
439 // Returns NULL if not found
440 ClientActiveObject * getSelectedActiveObject(
442 v3f from_pos_f_on_map,
443 core::line3d<f32> shootline_on_map
446 std::list<std::string> getConnectedPlayerNames();
448 float getAnimationTime();
451 void setCrack(int level, v3s16 pos);
453 void setHighlighted(v3s16 pos, bool show_higlighted);
454 v3s16 getHighlighted(){ return m_highlighted_pos; }
459 bool checkPrivilege(const std::string &priv)
460 { return (m_privileges.count(priv) != 0); }
462 bool getChatMessage(std::wstring &message);
463 void typeChatMessage(const std::wstring& message);
465 u64 getMapSeed(){ return m_map_seed; }
467 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
468 // Including blocks at appropriate edges
469 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
470 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
472 void updateCameraOffset(v3s16 camera_offset)
473 { m_mesh_update_thread.m_camera_offset = camera_offset; }
475 // Get event from queue. CE_NONE is returned if queue is empty.
476 ClientEvent getClientEvent();
479 { return m_access_denied; }
481 std::wstring accessDeniedReason()
482 { return m_access_denied_reason; }
484 bool itemdefReceived()
485 { return m_itemdef_received; }
486 bool nodedefReceived()
487 { return m_nodedef_received; }
489 { return m_media_downloader == NULL; }
491 float mediaReceiveProgress();
493 void afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font);
496 float getCurRate(void);
497 float getAvgRate(void);
499 // IGameDef interface
500 virtual IItemDefManager* getItemDefManager();
501 virtual INodeDefManager* getNodeDefManager();
502 virtual ICraftDefManager* getCraftDefManager();
503 virtual ITextureSource* getTextureSource();
504 virtual IShaderSource* getShaderSource();
505 virtual scene::ISceneManager* getSceneManager();
506 virtual u16 allocateUnknownNodeId(const std::string &name);
507 virtual ISoundManager* getSoundManager();
508 virtual MtEventManager* getEventManager();
509 virtual ParticleManager* getParticleManager();
510 virtual bool checkLocalPrivilege(const std::string &priv)
511 { return checkPrivilege(priv); }
512 virtual scene::IAnimatedMesh* getMesh(const std::string &filename);
514 // The following set of functions is used by ClientMediaDownloader
515 // Insert a media file appropriately into the appropriate manager
516 bool loadMedia(const std::string &data, const std::string &filename);
517 // Send a request for conventional media transfer
518 void request_media(const std::vector<std::string> &file_requests);
519 // Send a notification that no conventional media transfer is needed
520 void received_media();
522 LocalClientState getState() { return m_state; }
524 void makeScreenshot(IrrlichtDevice *device);
528 // Virtual methods from con::PeerHandler
529 void peerAdded(con::Peer *peer);
530 void deletingPeer(con::Peer *peer, bool timeout);
532 void initLocalMapSaving(const Address &address,
533 const std::string &hostname,
534 bool is_local_server);
539 void sendPlayerPos();
540 // Send the item number 'item' as player item to the server
541 void sendPlayerItem(u16 item);
543 float m_packetcounter_timer;
544 float m_connection_reinit_timer;
545 float m_avg_rtt_timer;
546 float m_playerpos_send_timer;
547 float m_ignore_damage_timer; // Used after server moves player
548 IntervalLimiter m_map_timer_and_unload_interval;
550 IWritableTextureSource *m_tsrc;
551 IWritableShaderSource *m_shsrc;
552 IWritableItemDefManager *m_itemdef;
553 IWritableNodeDefManager *m_nodedef;
554 ISoundManager *m_sound;
555 MtEventManager *m_event;
558 MeshUpdateThread m_mesh_update_thread;
559 ClientEnvironment m_env;
560 ParticleManager m_particle_manager;
561 con::Connection m_con;
562 IrrlichtDevice *m_device;
563 // Server serialization version
566 bool m_inventory_updated;
567 Inventory *m_inventory_from_server;
568 float m_inventory_from_server_age;
569 std::set<v3s16> m_active_blocks;
570 PacketCounter m_packetcounter;
571 bool m_show_highlighted;
572 // Block mesh animation parameters
573 float m_animation_time;
576 v3s16 m_highlighted_pos;
577 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
579 //u32 m_daynight_ratio;
580 std::queue<std::wstring> m_chat_queue;
581 // The seed returned by the server in TOCLIENT_INIT is stored here
583 std::string m_password;
584 bool m_access_denied;
585 std::wstring m_access_denied_reason;
586 std::queue<ClientEvent> m_client_event_queue;
587 bool m_itemdef_received;
588 bool m_nodedef_received;
589 ClientMediaDownloader *m_media_downloader;
591 // time_of_day speed approximation for old protocol
592 bool m_time_of_day_set;
593 float m_last_time_of_day_f;
594 float m_time_of_day_update_timer;
596 // An interval for generally sending object positions and stuff
597 float m_recommended_send_interval;
600 float m_removed_sounds_check_timer;
601 // Mapping from server sound ids to our sound ids
602 std::map<s32, int> m_sounds_server_to_client;
603 // And the other way!
604 std::map<int, s32> m_sounds_client_to_server;
605 // And relations to objects
606 std::map<int, u16> m_sounds_to_objects;
609 std::set<std::string> m_privileges;
611 // Detached inventories
613 std::map<std::string, Inventory*> m_detached_inventories;
615 // Storage for mesh data for creating multiple instances of the same mesh
616 std::map<std::string, std::string> m_mesh_data;
619 LocalClientState m_state;
621 // Used for saving server map to disk client-side
623 IntervalLimiter m_localdb_save_interval;
624 u16 m_cache_save_interval;
626 // TODO: Add callback to update these when g_settings changes
627 bool m_cache_smooth_lighting;
628 bool m_cache_enable_shaders;
631 #endif // !CLIENT_HEADER