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.
22 #include "clientenvironment.h"
23 #include "irrlichttypes_extrabloated.h"
28 #include <unordered_set>
29 #include "clientobject.h"
31 #include "inventorymanager.h"
32 #include "localplayer.h"
34 #include "particles.h"
36 #include "tileanimation.h"
37 #include "mesh_generator_thread.h"
38 #include "network/address.h"
39 #include "network/peerhandler.h"
42 #define CLIENT_CHAT_MESSAGE_LIMIT_PER_10S 10.0f
47 class IWritableTextureSource;
48 class IWritableShaderSource;
49 class IWritableItemDefManager;
50 class IWritableNodeDefManager;
51 //class IWritableCraftDefManager;
52 class ClientMediaDownloader;
53 struct MapDrawControl;
58 struct MinimapMapblock;
65 enum LocalClientState {
78 CE_SHOW_LOCAL_FORMSPEC,
80 CE_ADD_PARTICLESPAWNER,
81 CE_DELETE_PARTICLESPAWNER,
86 CE_OVERRIDE_DAY_NIGHT_RATIO,
104 bool set_camera_point_target;
105 f32 camera_point_target_x;
106 f32 camera_point_target_y;
107 f32 camera_point_target_z;
110 std::string *formspec;
111 std::string *formname;
114 //} textures_updated;
121 bool collisiondetection;
122 bool collision_removal;
124 std::string *texture;
125 struct TileAnimationParams animation;
141 bool collisiondetection;
142 bool collision_removal;
145 std::string *texture;
147 struct TileAnimationParams animation;
149 } add_particlespawner;
152 } delete_particlespawner;
181 video::SColor *bgcolor;
183 std::vector<std::string> *params;
189 } override_day_night_ratio;
209 PacketCounter() = default;
211 void add(u16 command)
213 std::map<u16, u16>::iterator n = m_packets.find(command);
214 if(n == m_packets.end())
216 m_packets[command] = 1;
226 for (auto &m_packet : m_packets) {
231 void print(std::ostream &o)
233 for (const auto &m_packet : m_packets) {
234 o << "cmd "<< m_packet.first <<" count "<< m_packet.second << std::endl;
240 std::map<u16, u16> m_packets;
243 class ClientScripting;
246 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
250 NOTE: Nothing is thread-safe here.
254 const char *playername,
255 const std::string &password,
256 const std::string &address_name,
257 MapDrawControl &control,
258 IWritableTextureSource *tsrc,
259 IWritableShaderSource *shsrc,
260 IWritableItemDefManager *itemdef,
261 IWritableNodeDefManager *nodedef,
262 ISoundManager *sound,
263 MtEventManager *event,
265 GameUIFlags *game_ui_flags
269 DISABLE_CLASS_COPY(Client);
271 // Load local mods into memory
273 void scanModSubfolder(const std::string &mod_name, const std::string &mod_path,
274 std::string mod_subpath);
275 inline void scanModIntoMemory(const std::string &mod_name, const std::string &mod_path)
277 scanModSubfolder(mod_name, mod_path, "");
284 request all threads managed by client to be stopped
292 The name of the local player should already be set when
293 calling this, as it is sent in the initialization.
295 void connect(Address address, bool is_local_server);
298 Stuff that references the environment is valid only as
299 long as this is not called. (eg. Players)
300 If this throws a PeerNotFoundException, the connection has
303 void step(float dtime);
309 void handleCommand(NetworkPacket* pkt);
311 void handleCommand_Null(NetworkPacket* pkt) {};
312 void handleCommand_Deprecated(NetworkPacket* pkt);
313 void handleCommand_Hello(NetworkPacket* pkt);
314 void handleCommand_AuthAccept(NetworkPacket* pkt);
315 void handleCommand_AcceptSudoMode(NetworkPacket* pkt);
316 void handleCommand_DenySudoMode(NetworkPacket* pkt);
317 void handleCommand_InitLegacy(NetworkPacket* pkt);
318 void handleCommand_AccessDenied(NetworkPacket* pkt);
319 void handleCommand_RemoveNode(NetworkPacket* pkt);
320 void handleCommand_AddNode(NetworkPacket* pkt);
321 void handleCommand_BlockData(NetworkPacket* pkt);
322 void handleCommand_Inventory(NetworkPacket* pkt);
323 void handleCommand_TimeOfDay(NetworkPacket* pkt);
324 void handleCommand_ChatMessageOld(NetworkPacket *pkt);
325 void handleCommand_ChatMessage(NetworkPacket *pkt);
326 void handleCommand_ActiveObjectRemoveAdd(NetworkPacket* pkt);
327 void handleCommand_ActiveObjectMessages(NetworkPacket* pkt);
328 void handleCommand_Movement(NetworkPacket* pkt);
329 void handleCommand_HP(NetworkPacket* pkt);
330 void handleCommand_Breath(NetworkPacket* pkt);
331 void handleCommand_MovePlayer(NetworkPacket* pkt);
332 void handleCommand_DeathScreen(NetworkPacket* pkt);
333 void handleCommand_AnnounceMedia(NetworkPacket* pkt);
334 void handleCommand_Media(NetworkPacket* pkt);
335 void handleCommand_NodeDef(NetworkPacket* pkt);
336 void handleCommand_ItemDef(NetworkPacket* pkt);
337 void handleCommand_PlaySound(NetworkPacket* pkt);
338 void handleCommand_StopSound(NetworkPacket* pkt);
339 void handleCommand_FadeSound(NetworkPacket *pkt);
340 void handleCommand_Privileges(NetworkPacket* pkt);
341 void handleCommand_InventoryFormSpec(NetworkPacket* pkt);
342 void handleCommand_DetachedInventory(NetworkPacket* pkt);
343 void handleCommand_ShowFormSpec(NetworkPacket* pkt);
344 void handleCommand_SpawnParticle(NetworkPacket* pkt);
345 void handleCommand_AddParticleSpawner(NetworkPacket* pkt);
346 void handleCommand_DeleteParticleSpawner(NetworkPacket* pkt);
347 void handleCommand_HudAdd(NetworkPacket* pkt);
348 void handleCommand_HudRemove(NetworkPacket* pkt);
349 void handleCommand_HudChange(NetworkPacket* pkt);
350 void handleCommand_HudSetFlags(NetworkPacket* pkt);
351 void handleCommand_HudSetParam(NetworkPacket* pkt);
352 void handleCommand_HudSetSky(NetworkPacket* pkt);
353 void handleCommand_CloudParams(NetworkPacket* pkt);
354 void handleCommand_OverrideDayNightRatio(NetworkPacket* pkt);
355 void handleCommand_LocalPlayerAnimations(NetworkPacket* pkt);
356 void handleCommand_EyeOffset(NetworkPacket* pkt);
357 void handleCommand_UpdatePlayerList(NetworkPacket* pkt);
358 void handleCommand_SrpBytesSandB(NetworkPacket* pkt);
359 void handleCommand_CSMFlavourLimits(NetworkPacket *pkt);
361 void ProcessData(NetworkPacket *pkt);
363 void Send(NetworkPacket* pkt);
365 void interact(u8 action, const PointedThing& pointed);
367 void sendNodemetaFields(v3s16 p, const std::string &formname,
368 const StringMap &fields);
369 void sendInventoryFields(const std::string &formname,
370 const StringMap &fields);
371 void sendInventoryAction(InventoryAction *a);
372 void sendChatMessage(const std::wstring &message);
373 void clearOutChatQueue();
374 void sendChangePassword(const std::string &oldpassword,
375 const std::string &newpassword);
376 void sendDamage(u8 damage);
377 void sendBreath(u16 breath);
381 ClientEnvironment& getEnv() { return m_env; }
382 ITextureSource *tsrc() { return getTextureSource(); }
383 ISoundManager *sound() { return getSoundManager(); }
384 static const std::string &getBuiltinLuaPath();
385 static const std::string &getClientModsLuaPath();
387 virtual const std::vector<ModSpec> &getMods() const;
388 virtual const ModSpec* getModSpec(const std::string &modname) const;
390 // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
391 void removeNode(v3s16 p);
394 * Helper function for Client Side Modding
395 * Flavour is applied there, this should not be used for core engine
397 * @param is_valid_position
400 MapNode getNode(v3s16 p, bool *is_valid_position);
401 void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
403 void setPlayerControl(PlayerControl &control);
405 void selectPlayerItem(u16 item);
406 u16 getPlayerItem() const
407 { return m_playeritem; }
409 // Returns true if the inventory of the local player has been
410 // updated from the server. If it is true, it is set to false.
411 bool getLocalInventoryUpdated();
412 // Copies the inventory of the local player to parameter
413 void getLocalInventory(Inventory &dst);
415 /* InventoryManager interface */
416 Inventory* getInventory(const InventoryLocation &loc);
417 void inventoryAction(InventoryAction *a);
419 const std::list<std::string> &getConnectedPlayerNames()
421 return m_env.getPlayerNames();
424 float getAnimationTime();
428 void setCrack(int level, v3s16 pos);
432 bool checkPrivilege(const std::string &priv) const
433 { return (m_privileges.count(priv) != 0); }
435 const std::unordered_set<std::string> &getPrivilegeList() const
436 { return m_privileges; }
438 bool getChatMessage(std::wstring &message);
439 void typeChatMessage(const std::wstring& message);
441 u64 getMapSeed(){ return m_map_seed; }
443 void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
444 // Including blocks at appropriate edges
445 void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
446 void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
448 void updateCameraOffset(v3s16 camera_offset)
449 { m_mesh_update_thread.m_camera_offset = camera_offset; }
451 bool hasClientEvents() const { return !m_client_event_queue.empty(); }
452 // Get event from queue. If queue is empty, it triggers an assertion failure.
453 ClientEvent getClientEvent();
455 bool accessDenied() const { return m_access_denied; }
457 bool reconnectRequested() const { return m_access_denied_reconnect; }
459 void setFatalError(const std::string &reason)
461 m_access_denied = true;
462 m_access_denied_reason = reason;
465 // Renaming accessDeniedReason to better name could be good as it's used to
466 // disconnect client when CSM failed.
467 const std::string &accessDeniedReason() const { return m_access_denied_reason; }
469 bool itemdefReceived()
470 { return m_itemdef_received; }
471 bool nodedefReceived()
472 { return m_nodedef_received; }
474 { return !m_media_downloader; }
477 { return m_proto_ver; }
479 bool connectedToServer();
481 float mediaReceiveProgress();
483 void afterContentReceived();
488 Minimap* getMinimap() { return m_minimap; }
489 void setCamera(Camera* camera) { m_camera = camera; }
491 Camera* getCamera () { return m_camera; }
493 bool shouldShowMinimap() const;
495 // IGameDef interface
496 virtual IItemDefManager* getItemDefManager();
497 virtual INodeDefManager* getNodeDefManager();
498 virtual ICraftDefManager* getCraftDefManager();
499 ITextureSource* getTextureSource();
500 virtual IShaderSource* getShaderSource();
501 IShaderSource *shsrc() { return getShaderSource(); }
502 virtual u16 allocateUnknownNodeId(const std::string &name);
503 virtual ISoundManager* getSoundManager();
504 virtual MtEventManager* getEventManager();
505 virtual ParticleManager* getParticleManager();
506 bool checkLocalPrivilege(const std::string &priv)
507 { return checkPrivilege(priv); }
508 virtual scene::IAnimatedMesh* getMesh(const std::string &filename);
509 const std::string* getModFile(const std::string &filename);
511 virtual std::string getModStoragePath() const;
512 virtual bool registerModStorage(ModMetadata *meta);
513 virtual void unregisterModStorage(const std::string &name);
515 // The following set of functions is used by ClientMediaDownloader
516 // Insert a media file appropriately into the appropriate manager
517 bool loadMedia(const std::string &data, const std::string &filename);
518 // Send a request for conventional media transfer
519 void request_media(const std::vector<std::string> &file_requests);
521 LocalClientState getState() { return m_state; }
523 void makeScreenshot();
525 inline void pushToChatQueue(ChatMessage *cec)
527 m_chat_queue.push(cec);
530 ClientScripting *getScript() { return m_script; }
531 const bool moddingEnabled() const { return m_modding_enabled; }
533 inline void pushToEventQueue(const ClientEvent &event)
535 m_client_event_queue.push(event);
538 void showGameChat(bool show = true);
539 void showGameHud(bool show = true);
540 void showMinimap(bool show = true);
541 void showProfiler(bool show = true);
542 void showGameFog(bool show = true);
543 void showGameDebug(bool show = true);
545 const Address getServerAddress();
547 const std::string &getAddressName() const
549 return m_address_name;
552 inline bool checkCSMFlavourLimit(CSMFlavourLimit flag) const
554 return m_csm_flavour_limits & flag;
557 u32 getCSMNodeRangeLimit() const
559 return m_csm_noderange_limit;
564 // Virtual methods from con::PeerHandler
565 void peerAdded(con::Peer *peer);
566 void deletingPeer(con::Peer *peer, bool timeout);
568 void initLocalMapSaving(const Address &address,
569 const std::string &hostname,
570 bool is_local_server);
575 void sendPlayerPos();
576 // Send the item number 'item' as player item to the server
577 void sendPlayerItem(u16 item);
579 void deleteAuthData();
580 // helper method shared with clientpackethandler
581 static AuthMechanism choseAuthMech(const u32 mechs);
583 void sendLegacyInit(const char* playerName, const char* playerPassword);
584 void sendInit(const std::string &playerName);
585 void startAuth(AuthMechanism chosen_auth_mechanism);
586 void sendDeletedBlocks(std::vector<v3s16> &blocks);
587 void sendGotBlocks(v3s16 block);
588 void sendRemovedSounds(std::vector<s32> &soundList);
591 inline std::string getPlayerName()
592 { return m_env.getLocalPlayer()->getName(); }
594 bool canSendChatMessage() const;
596 float m_packetcounter_timer = 0.0f;
597 float m_connection_reinit_timer = 0.1f;
598 float m_avg_rtt_timer = 0.0f;
599 float m_playerpos_send_timer = 0.0f;
600 float m_ignore_damage_timer = 0.0f; // Used after server moves player
601 IntervalLimiter m_map_timer_and_unload_interval;
603 IWritableTextureSource *m_tsrc;
604 IWritableShaderSource *m_shsrc;
605 IWritableItemDefManager *m_itemdef;
606 IWritableNodeDefManager *m_nodedef;
607 ISoundManager *m_sound;
608 MtEventManager *m_event;
611 MeshUpdateThread m_mesh_update_thread;
612 ClientEnvironment m_env;
613 ParticleManager m_particle_manager;
614 std::unique_ptr<con::Connection> m_con;
615 std::string m_address_name;
616 Camera *m_camera = nullptr;
617 Minimap *m_minimap = nullptr;
618 bool m_minimap_disabled_by_server = false;
619 // Server serialization version
622 // Used version of the protocol with server
623 // Values smaller than 25 only mean they are smaller than 25,
624 // and aren't accurate. We simply just don't know, because
625 // the server didn't send the version back then.
626 // If 0, server init hasn't been received yet.
629 u16 m_playeritem = 0;
630 bool m_inventory_updated = false;
631 Inventory *m_inventory_from_server = nullptr;
632 float m_inventory_from_server_age = 0.0f;
633 PacketCounter m_packetcounter;
634 // Block mesh animation parameters
635 float m_animation_time = 0.0f;
636 int m_crack_level = -1;
638 // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
640 //u32 m_daynight_ratio;
641 std::queue<std::wstring> m_out_chat_queue;
642 u32 m_last_chat_message_sent;
643 float m_chat_message_allowance = 5.0f;
644 std::queue<ChatMessage *> m_chat_queue;
646 // The authentication methods we can use to enter sudo mode (=change password)
647 u32 m_sudo_auth_methods;
649 // The seed returned by the server in TOCLIENT_INIT is stored here
653 std::string m_playername;
654 std::string m_password;
655 // If set, this will be sent (and cleared) upon a TOCLIENT_ACCEPT_SUDO_MODE
656 std::string m_new_password;
657 // Usable by auth mechanisms.
658 AuthMechanism m_chosen_auth_mech;
659 void *m_auth_data = nullptr;
662 bool m_access_denied = false;
663 bool m_access_denied_reconnect = false;
664 std::string m_access_denied_reason = "";
665 std::queue<ClientEvent> m_client_event_queue;
666 bool m_itemdef_received = false;
667 bool m_nodedef_received = false;
668 ClientMediaDownloader *m_media_downloader;
670 // time_of_day speed approximation for old protocol
671 bool m_time_of_day_set = false;
672 float m_last_time_of_day_f = -1.0f;
673 float m_time_of_day_update_timer = 0.0f;
675 // An interval for generally sending object positions and stuff
676 float m_recommended_send_interval = 0.1f;
679 float m_removed_sounds_check_timer = 0.0f;
680 // Mapping from server sound ids to our sound ids
681 std::unordered_map<s32, int> m_sounds_server_to_client;
682 // And the other way!
683 std::unordered_map<int, s32> m_sounds_client_to_server;
684 // And relations to objects
685 std::unordered_map<int, u16> m_sounds_to_objects;
688 std::unordered_set<std::string> m_privileges;
690 // Detached inventories
692 std::unordered_map<std::string, Inventory*> m_detached_inventories;
694 // Storage for mesh data for creating multiple instances of the same mesh
695 StringMap m_mesh_data;
697 StringMap m_mod_files;
700 LocalClientState m_state;
702 // Used for saving server map to disk client-side
703 MapDatabase *m_localdb = nullptr;
704 IntervalLimiter m_localdb_save_interval;
705 u16 m_cache_save_interval;
707 ClientScripting *m_script = nullptr;
708 bool m_modding_enabled;
709 std::unordered_map<std::string, ModMetadata *> m_mod_storages;
710 float m_mod_storage_save_timer = 10.0f;
711 std::vector<ModSpec> m_mods;
712 GameUIFlags *m_game_ui_flags;
714 bool m_shutdown = false;
716 // CSM flavour limits byteflag
717 u64 m_csm_flavour_limits = CSMFlavourLimit::CSM_FL_NONE;
718 u32 m_csm_noderange_limit = 8;