client.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #ifndef CLIENT_HEADER
  17. #define CLIENT_HEADER
  18. #include "connection.h"
  19. #include "environment.h"
  20. #include "irrlichttypes_extrabloated.h"
  21. #include "jthread/jmutex.h"
  22. #include <ostream>
  23. #include <map>
  24. #include <set>
  25. #include <vector>
  26. #include "clientobject.h"
  27. #include "gamedef.h"
  28. #include "inventorymanager.h"
  29. #include "localplayer.h"
  30. #include "hud.h"
  31. #include "particles.h"
  32. struct MeshMakeData;
  33. class MapBlockMesh;
  34. class IWritableTextureSource;
  35. class IWritableShaderSource;
  36. class IWritableItemDefManager;
  37. class IWritableNodeDefManager;
  38. //class IWritableCraftDefManager;
  39. class ClientMediaDownloader;
  40. struct MapDrawControl;
  41. class MtEventManager;
  42. struct PointedThing;
  43. struct QueuedMeshUpdate
  44. {
  45. v3s16 p;
  46. MeshMakeData *data;
  47. bool ack_block_to_server;
  48. QueuedMeshUpdate();
  49. ~QueuedMeshUpdate();
  50. };
  51. enum LocalClientState {
  52. LC_Created,
  53. LC_Init,
  54. LC_Ready
  55. };
  56. /*
  57. A thread-safe queue of mesh update tasks
  58. */
  59. class MeshUpdateQueue
  60. {
  61. public:
  62. MeshUpdateQueue();
  63. ~MeshUpdateQueue();
  64. /*
  65. peer_id=0 adds with nobody to send to
  66. */
  67. void addBlock(v3s16 p, MeshMakeData *data,
  68. bool ack_block_to_server, bool urgent);
  69. // Returned pointer must be deleted
  70. // Returns NULL if queue is empty
  71. QueuedMeshUpdate * pop();
  72. u32 size()
  73. {
  74. JMutexAutoLock lock(m_mutex);
  75. return m_queue.size();
  76. }
  77. private:
  78. std::vector<QueuedMeshUpdate*> m_queue;
  79. std::set<v3s16> m_urgents;
  80. JMutex m_mutex;
  81. };
  82. struct MeshUpdateResult
  83. {
  84. v3s16 p;
  85. MapBlockMesh *mesh;
  86. bool ack_block_to_server;
  87. MeshUpdateResult():
  88. p(-1338,-1338,-1338),
  89. mesh(NULL),
  90. ack_block_to_server(false)
  91. {
  92. }
  93. };
  94. class MeshUpdateThread : public JThread
  95. {
  96. public:
  97. MeshUpdateThread(IGameDef *gamedef):
  98. m_gamedef(gamedef)
  99. {
  100. }
  101. void * Thread();
  102. MeshUpdateQueue m_queue_in;
  103. MutexedQueue<MeshUpdateResult> m_queue_out;
  104. IGameDef *m_gamedef;
  105. v3s16 m_camera_offset;
  106. };
  107. enum ClientEventType
  108. {
  109. CE_NONE,
  110. CE_PLAYER_DAMAGE,
  111. CE_PLAYER_FORCE_MOVE,
  112. CE_DEATHSCREEN,
  113. CE_SHOW_FORMSPEC,
  114. CE_SPAWN_PARTICLE,
  115. CE_ADD_PARTICLESPAWNER,
  116. CE_DELETE_PARTICLESPAWNER,
  117. CE_HUDADD,
  118. CE_HUDRM,
  119. CE_HUDCHANGE,
  120. CE_SET_SKY,
  121. CE_OVERRIDE_DAY_NIGHT_RATIO,
  122. };
  123. struct ClientEvent
  124. {
  125. ClientEventType type;
  126. union{
  127. struct{
  128. } none;
  129. struct{
  130. u8 amount;
  131. } player_damage;
  132. struct{
  133. f32 pitch;
  134. f32 yaw;
  135. } player_force_move;
  136. struct{
  137. bool set_camera_point_target;
  138. f32 camera_point_target_x;
  139. f32 camera_point_target_y;
  140. f32 camera_point_target_z;
  141. } deathscreen;
  142. struct{
  143. std::string *formspec;
  144. std::string *formname;
  145. } show_formspec;
  146. struct{
  147. } textures_updated;
  148. struct{
  149. v3f *pos;
  150. v3f *vel;
  151. v3f *acc;
  152. f32 expirationtime;
  153. f32 size;
  154. bool collisiondetection;
  155. bool vertical;
  156. std::string *texture;
  157. } spawn_particle;
  158. struct{
  159. u16 amount;
  160. f32 spawntime;
  161. v3f *minpos;
  162. v3f *maxpos;
  163. v3f *minvel;
  164. v3f *maxvel;
  165. v3f *minacc;
  166. v3f *maxacc;
  167. f32 minexptime;
  168. f32 maxexptime;
  169. f32 minsize;
  170. f32 maxsize;
  171. bool collisiondetection;
  172. bool vertical;
  173. std::string *texture;
  174. u32 id;
  175. } add_particlespawner;
  176. struct{
  177. u32 id;
  178. } delete_particlespawner;
  179. struct{
  180. u32 id;
  181. u8 type;
  182. v2f *pos;
  183. std::string *name;
  184. v2f *scale;
  185. std::string *text;
  186. u32 number;
  187. u32 item;
  188. u32 dir;
  189. v2f *align;
  190. v2f *offset;
  191. v3f *world_pos;
  192. v2s32 * size;
  193. } hudadd;
  194. struct{
  195. u32 id;
  196. } hudrm;
  197. struct{
  198. u32 id;
  199. HudElementStat stat;
  200. v2f *v2fdata;
  201. std::string *sdata;
  202. u32 data;
  203. v3f *v3fdata;
  204. v2s32 * v2s32data;
  205. } hudchange;
  206. struct{
  207. video::SColor *bgcolor;
  208. std::string *type;
  209. std::vector<std::string> *params;
  210. } set_sky;
  211. struct{
  212. bool do_override;
  213. float ratio_f;
  214. } override_day_night_ratio;
  215. };
  216. };
  217. /*
  218. Packet counter
  219. */
  220. class PacketCounter
  221. {
  222. public:
  223. PacketCounter()
  224. {
  225. }
  226. void add(u16 command)
  227. {
  228. std::map<u16, u16>::iterator n = m_packets.find(command);
  229. if(n == m_packets.end())
  230. {
  231. m_packets[command] = 1;
  232. }
  233. else
  234. {
  235. n->second++;
  236. }
  237. }
  238. void clear()
  239. {
  240. for(std::map<u16, u16>::iterator
  241. i = m_packets.begin();
  242. i != m_packets.end(); ++i)
  243. {
  244. i->second = 0;
  245. }
  246. }
  247. void print(std::ostream &o)
  248. {
  249. for(std::map<u16, u16>::iterator
  250. i = m_packets.begin();
  251. i != m_packets.end(); ++i)
  252. {
  253. o<<"cmd "<<i->first
  254. <<" count "<<i->second
  255. <<std::endl;
  256. }
  257. }
  258. private:
  259. // command, count
  260. std::map<u16, u16> m_packets;
  261. };
  262. class Client : public con::PeerHandler, public InventoryManager, public IGameDef
  263. {
  264. public:
  265. /*
  266. NOTE: Nothing is thread-safe here.
  267. */
  268. Client(
  269. IrrlichtDevice *device,
  270. const char *playername,
  271. std::string password,
  272. MapDrawControl &control,
  273. IWritableTextureSource *tsrc,
  274. IWritableShaderSource *shsrc,
  275. IWritableItemDefManager *itemdef,
  276. IWritableNodeDefManager *nodedef,
  277. ISoundManager *sound,
  278. MtEventManager *event,
  279. bool ipv6
  280. );
  281. ~Client();
  282. /*
  283. request all threads managed by client to be stopped
  284. */
  285. void Stop();
  286. bool isShutdown();
  287. /*
  288. The name of the local player should already be set when
  289. calling this, as it is sent in the initialization.
  290. */
  291. void connect(Address address);
  292. /*
  293. Stuff that references the environment is valid only as
  294. long as this is not called. (eg. Players)
  295. If this throws a PeerNotFoundException, the connection has
  296. timed out.
  297. */
  298. void step(float dtime);
  299. void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
  300. // Returns true if something was received
  301. bool AsyncProcessPacket();
  302. bool AsyncProcessData();
  303. void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
  304. void interact(u8 action, const PointedThing& pointed);
  305. void sendNodemetaFields(v3s16 p, const std::string &formname,
  306. const std::map<std::string, std::string> &fields);
  307. void sendInventoryFields(const std::string &formname,
  308. const std::map<std::string, std::string> &fields);
  309. void sendInventoryAction(InventoryAction *a);
  310. void sendChatMessage(const std::wstring &message);
  311. void sendChangePassword(const std::wstring &oldpassword,
  312. const std::wstring &newpassword);
  313. void sendDamage(u8 damage);
  314. void sendBreath(u16 breath);
  315. void sendRespawn();
  316. void sendReady();
  317. ClientEnvironment& getEnv()
  318. { return m_env; }
  319. // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
  320. void removeNode(v3s16 p);
  321. void addNode(v3s16 p, MapNode n, bool remove_metadata = true);
  322. void setPlayerControl(PlayerControl &control);
  323. void selectPlayerItem(u16 item);
  324. u16 getPlayerItem() const
  325. { return m_playeritem; }
  326. // Returns true if the inventory of the local player has been
  327. // updated from the server. If it is true, it is set to false.
  328. bool getLocalInventoryUpdated();
  329. // Copies the inventory of the local player to parameter
  330. void getLocalInventory(Inventory &dst);
  331. /* InventoryManager interface */
  332. Inventory* getInventory(const InventoryLocation &loc);
  333. void inventoryAction(InventoryAction *a);
  334. // Gets closest object pointed by the shootline
  335. // Returns NULL if not found
  336. ClientActiveObject * getSelectedActiveObject(
  337. f32 max_d,
  338. v3f from_pos_f_on_map,
  339. core::line3d<f32> shootline_on_map
  340. );
  341. std::list<std::string> getConnectedPlayerNames();
  342. float getAnimationTime();
  343. int getCrackLevel();
  344. void setCrack(int level, v3s16 pos);
  345. void setHighlighted(v3s16 pos, bool show_hud);
  346. v3s16 getHighlighted(){ return m_highlighted_pos; }
  347. u16 getHP();
  348. u16 getBreath();
  349. bool checkPrivilege(const std::string &priv)
  350. { return (m_privileges.count(priv) != 0); }
  351. bool getChatMessage(std::wstring &message);
  352. void typeChatMessage(const std::wstring& message);
  353. u64 getMapSeed(){ return m_map_seed; }
  354. void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
  355. // Including blocks at appropriate edges
  356. void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
  357. void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
  358. void updateCameraOffset(v3s16 camera_offset)
  359. { m_mesh_update_thread.m_camera_offset = camera_offset; }
  360. // Get event from queue. CE_NONE is returned if queue is empty.
  361. ClientEvent getClientEvent();
  362. bool accessDenied()
  363. { return m_access_denied; }
  364. std::wstring accessDeniedReason()
  365. { return m_access_denied_reason; }
  366. bool itemdefReceived()
  367. { return m_itemdef_received; }
  368. bool nodedefReceived()
  369. { return m_nodedef_received; }
  370. bool mediaReceived()
  371. { return m_media_downloader == NULL; }
  372. float mediaReceiveProgress();
  373. void afterContentReceived(IrrlichtDevice *device, gui::IGUIFont* font);
  374. float getRTT(void);
  375. float getCurRate(void);
  376. float getAvgRate(void);
  377. // IGameDef interface
  378. virtual IItemDefManager* getItemDefManager();
  379. virtual INodeDefManager* getNodeDefManager();
  380. virtual ICraftDefManager* getCraftDefManager();
  381. virtual ITextureSource* getTextureSource();
  382. virtual IShaderSource* getShaderSource();
  383. virtual u16 allocateUnknownNodeId(const std::string &name);
  384. virtual ISoundManager* getSoundManager();
  385. virtual MtEventManager* getEventManager();
  386. virtual bool checkLocalPrivilege(const std::string &priv)
  387. { return checkPrivilege(priv); }
  388. virtual scene::IAnimatedMesh* getMesh(const std::string &filename);
  389. // The following set of functions is used by ClientMediaDownloader
  390. // Insert a media file appropriately into the appropriate manager
  391. bool loadMedia(const std::string &data, const std::string &filename);
  392. // Send a request for conventional media transfer
  393. void request_media(const std::list<std::string> &file_requests);
  394. // Send a notification that no conventional media transfer is needed
  395. void received_media();
  396. LocalClientState getState() { return m_state; }
  397. private:
  398. // Virtual methods from con::PeerHandler
  399. void peerAdded(con::Peer *peer);
  400. void deletingPeer(con::Peer *peer, bool timeout);
  401. void ReceiveAll();
  402. void Receive();
  403. void sendPlayerPos();
  404. // Send the item number 'item' as player item to the server
  405. void sendPlayerItem(u16 item);
  406. float m_packetcounter_timer;
  407. float m_connection_reinit_timer;
  408. float m_avg_rtt_timer;
  409. float m_playerpos_send_timer;
  410. float m_ignore_damage_timer; // Used after server moves player
  411. IntervalLimiter m_map_timer_and_unload_interval;
  412. IWritableTextureSource *m_tsrc;
  413. IWritableShaderSource *m_shsrc;
  414. IWritableItemDefManager *m_itemdef;
  415. IWritableNodeDefManager *m_nodedef;
  416. ISoundManager *m_sound;
  417. MtEventManager *m_event;
  418. MeshUpdateThread m_mesh_update_thread;
  419. ClientEnvironment m_env;
  420. con::Connection m_con;
  421. IrrlichtDevice *m_device;
  422. // Server serialization version
  423. u8 m_server_ser_ver;
  424. u16 m_playeritem;
  425. bool m_inventory_updated;
  426. Inventory *m_inventory_from_server;
  427. float m_inventory_from_server_age;
  428. std::set<v3s16> m_active_blocks;
  429. PacketCounter m_packetcounter;
  430. bool m_show_hud;
  431. // Block mesh animation parameters
  432. float m_animation_time;
  433. int m_crack_level;
  434. v3s16 m_crack_pos;
  435. v3s16 m_highlighted_pos;
  436. // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
  437. //s32 m_daynight_i;
  438. //u32 m_daynight_ratio;
  439. Queue<std::wstring> m_chat_queue;
  440. // The seed returned by the server in TOCLIENT_INIT is stored here
  441. u64 m_map_seed;
  442. std::string m_password;
  443. bool m_access_denied;
  444. std::wstring m_access_denied_reason;
  445. Queue<ClientEvent> m_client_event_queue;
  446. bool m_itemdef_received;
  447. bool m_nodedef_received;
  448. ClientMediaDownloader *m_media_downloader;
  449. // time_of_day speed approximation for old protocol
  450. bool m_time_of_day_set;
  451. float m_last_time_of_day_f;
  452. float m_time_of_day_update_timer;
  453. // An interval for generally sending object positions and stuff
  454. float m_recommended_send_interval;
  455. // Sounds
  456. float m_removed_sounds_check_timer;
  457. // Mapping from server sound ids to our sound ids
  458. std::map<s32, int> m_sounds_server_to_client;
  459. // And the other way!
  460. std::map<int, s32> m_sounds_client_to_server;
  461. // And relations to objects
  462. std::map<int, u16> m_sounds_to_objects;
  463. // Privileges
  464. std::set<std::string> m_privileges;
  465. // Detached inventories
  466. // key = name
  467. std::map<std::string, Inventory*> m_detached_inventories;
  468. // Storage for mesh data for creating multiple instances of the same mesh
  469. std::map<std::string, std::string> m_mesh_data;
  470. // own state
  471. LocalClientState m_state;
  472. };
  473. #endif // !CLIENT_HEADER