f85e8ac7be643a5af87b0f7d049780e347675022
[oweals/minetest.git] / src / client.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef CLIENT_HEADER
21 #define CLIENT_HEADER
22
23 #include "connection.h"
24 #include "environment.h"
25 #include "irrlichttypes_extrabloated.h"
26 #include "jmutex.h"
27 #include <ostream>
28 #include <set>
29 #include <vector>
30 #include "clientobject.h"
31 #include "gamedef.h"
32 #include "inventorymanager.h"
33 #include "filesys.h"
34 #include "filecache.h"
35 #include "localplayer.h"
36 #include "util/pointedthing.h"
37
38 struct MeshMakeData;
39 class MapBlockMesh;
40 class IGameDef;
41 class IWritableTextureSource;
42 class IWritableShaderSource;
43 class IWritableItemDefManager;
44 class IWritableNodeDefManager;
45 //class IWritableCraftDefManager;
46 class ClientEnvironment;
47 struct MapDrawControl;
48 class MtEventManager;
49
50 class ClientNotReadyException : public BaseException
51 {
52 public:
53         ClientNotReadyException(const char *s):
54                 BaseException(s)
55         {}
56 };
57
58 struct QueuedMeshUpdate
59 {
60         v3s16 p;
61         MeshMakeData *data;
62         bool ack_block_to_server;
63
64         QueuedMeshUpdate();
65         ~QueuedMeshUpdate();
66 };
67
68 /*
69         A thread-safe queue of mesh update tasks
70 */
71 class MeshUpdateQueue
72 {
73 public:
74         MeshUpdateQueue();
75
76         ~MeshUpdateQueue();
77         
78         /*
79                 peer_id=0 adds with nobody to send to
80         */
81         void addBlock(v3s16 p, MeshMakeData *data,
82                         bool ack_block_to_server, bool urgent);
83
84         // Returned pointer must be deleted
85         // Returns NULL if queue is empty
86         QueuedMeshUpdate * pop();
87
88         u32 size()
89         {
90                 JMutexAutoLock lock(m_mutex);
91                 return m_queue.size();
92         }
93         
94 private:
95         std::vector<QueuedMeshUpdate*> m_queue;
96         std::set<v3s16> m_urgents;
97         JMutex m_mutex;
98 };
99
100 struct MeshUpdateResult
101 {
102         v3s16 p;
103         MapBlockMesh *mesh;
104         bool ack_block_to_server;
105
106         MeshUpdateResult():
107                 p(-1338,-1338,-1338),
108                 mesh(NULL),
109                 ack_block_to_server(false)
110         {
111         }
112 };
113
114 class MeshUpdateThread : public SimpleThread
115 {
116 public:
117
118         MeshUpdateThread(IGameDef *gamedef):
119                 m_gamedef(gamedef)
120         {
121         }
122
123         void * Thread();
124
125         MeshUpdateQueue m_queue_in;
126
127         MutexedQueue<MeshUpdateResult> m_queue_out;
128
129         IGameDef *m_gamedef;
130 };
131
132 enum ClientEventType
133 {
134         CE_NONE,
135         CE_PLAYER_DAMAGE,
136         CE_PLAYER_FORCE_MOVE,
137         CE_DEATHSCREEN,
138         CE_TEXTURES_UPDATED
139 };
140
141 struct ClientEvent
142 {
143         ClientEventType type;
144         union{
145                 struct{
146                 } none;
147                 struct{
148                         u8 amount;
149                 } player_damage;
150                 struct{
151                         f32 pitch;
152                         f32 yaw;
153                 } player_force_move;
154                 struct{
155                         bool set_camera_point_target;
156                         f32 camera_point_target_x;
157                         f32 camera_point_target_y;
158                         f32 camera_point_target_z;
159                 } deathscreen;
160                 struct{
161                 } textures_updated;
162         };
163 };
164
165 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
166 {
167 public:
168         /*
169                 NOTE: Nothing is thread-safe here.
170         */
171
172         Client(
173                         IrrlichtDevice *device,
174                         const char *playername,
175                         std::string password,
176                         MapDrawControl &control,
177                         IWritableTextureSource *tsrc,
178                         IWritableShaderSource *shsrc,
179                         IWritableItemDefManager *itemdef,
180                         IWritableNodeDefManager *nodedef,
181                         ISoundManager *sound,
182                         MtEventManager *event
183         );
184         
185         ~Client();
186         /*
187                 The name of the local player should already be set when
188                 calling this, as it is sent in the initialization.
189         */
190         void connect(Address address);
191         /*
192                 returns true when
193                         m_con.Connected() == true
194                         AND m_server_ser_ver != SER_FMT_VER_INVALID
195                 throws con::PeerNotFoundException if connection has been deleted,
196                 eg. timed out.
197         */
198         bool connectedAndInitialized();
199         /*
200                 Stuff that references the environment is valid only as
201                 long as this is not called. (eg. Players)
202                 If this throws a PeerNotFoundException, the connection has
203                 timed out.
204         */
205         void step(float dtime);
206
207         void ProcessData(u8 *data, u32 datasize, u16 sender_peer_id);
208         // Returns true if something was received
209         bool AsyncProcessPacket();
210         bool AsyncProcessData();
211         void Send(u16 channelnum, SharedBuffer<u8> data, bool reliable);
212
213         void interact(u8 action, const PointedThing& pointed);
214
215         void sendNodemetaFields(v3s16 p, const std::string &formname,
216                         const std::map<std::string, std::string> &fields);
217         void sendInventoryFields(const std::string &formname,
218                         const std::map<std::string, std::string> &fields);
219         void sendInventoryAction(InventoryAction *a);
220         void sendChatMessage(const std::wstring &message);
221         void sendChangePassword(const std::wstring oldpassword,
222                         const std::wstring newpassword);
223         void sendDamage(u8 damage);
224         void sendRespawn();
225
226         ClientEnvironment& getEnv()
227         { return m_env; }
228         
229         // Causes urgent mesh updates (unlike Map::add/removeNodeWithEvent)
230         void removeNode(v3s16 p);
231         void addNode(v3s16 p, MapNode n);
232         
233         void setPlayerControl(PlayerControl &control);
234
235         void selectPlayerItem(u16 item);
236         u16 getPlayerItem() const
237         { return m_playeritem; }
238
239         // Returns true if the inventory of the local player has been
240         // updated from the server. If it is true, it is set to false.
241         bool getLocalInventoryUpdated();
242         // Copies the inventory of the local player to parameter
243         void getLocalInventory(Inventory &dst);
244         
245         /* InventoryManager interface */
246         Inventory* getInventory(const InventoryLocation &loc);
247         void inventoryAction(InventoryAction *a);
248
249         // Gets closest object pointed by the shootline
250         // Returns NULL if not found
251         ClientActiveObject * getSelectedActiveObject(
252                         f32 max_d,
253                         v3f from_pos_f_on_map,
254                         core::line3d<f32> shootline_on_map
255         );
256
257         // Prints a line or two of info
258         void printDebugInfo(std::ostream &os);
259
260         core::list<std::wstring> getConnectedPlayerNames();
261
262         float getAnimationTime();
263
264         int getCrackLevel();
265         void setCrack(int level, v3s16 pos);
266
267         u16 getHP();
268
269         bool checkPrivilege(const std::string &priv)
270         { return (m_privileges.count(priv) != 0); }
271
272         bool getChatMessage(std::wstring &message);
273         void typeChatMessage(const std::wstring& message);
274
275         u64 getMapSeed(){ return m_map_seed; }
276
277         void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
278         // Including blocks at appropriate edges
279         void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false, bool urgent=false);
280         void addUpdateMeshTaskForNode(v3s16 nodepos, bool ack_to_server=false, bool urgent=false);
281
282         // Get event from queue. CE_NONE is returned if queue is empty.
283         ClientEvent getClientEvent();
284         
285         bool accessDenied()
286         { return m_access_denied; }
287
288         std::wstring accessDeniedReason()
289         { return m_access_denied_reason; }
290
291         float mediaReceiveProgress()
292         { return m_media_receive_progress; }
293
294         bool texturesReceived()
295         { return m_media_received; }
296         bool itemdefReceived()
297         { return m_itemdef_received; }
298         bool nodedefReceived()
299         { return m_nodedef_received; }
300         
301         void afterContentReceived();
302
303         float getRTT(void);
304
305         // IGameDef interface
306         virtual IItemDefManager* getItemDefManager();
307         virtual INodeDefManager* getNodeDefManager();
308         virtual ICraftDefManager* getCraftDefManager();
309         virtual ITextureSource* getTextureSource();
310         virtual IShaderSource* getShaderSource();
311         virtual u16 allocateUnknownNodeId(const std::string &name);
312         virtual ISoundManager* getSoundManager();
313         virtual MtEventManager* getEventManager();
314         virtual bool checkLocalPrivilege(const std::string &priv)
315         { return checkPrivilege(priv); }
316
317 private:
318         
319         // Insert a media file appropriately into the appropriate manager
320         bool loadMedia(const std::string &data, const std::string &filename);
321         
322         // Virtual methods from con::PeerHandler
323         void peerAdded(con::Peer *peer);
324         void deletingPeer(con::Peer *peer, bool timeout);
325         
326         void ReceiveAll();
327         void Receive();
328         
329         void sendPlayerPos();
330         // This sends the player's current name etc to the server
331         void sendPlayerInfo();
332         // Send the item number 'item' as player item to the server
333         void sendPlayerItem(u16 item);
334         
335         float m_packetcounter_timer;
336         float m_connection_reinit_timer;
337         float m_avg_rtt_timer;
338         float m_playerpos_send_timer;
339         float m_ignore_damage_timer; // Used after server moves player
340         IntervalLimiter m_map_timer_and_unload_interval;
341
342         IWritableTextureSource *m_tsrc;
343         IWritableShaderSource *m_shsrc;
344         IWritableItemDefManager *m_itemdef;
345         IWritableNodeDefManager *m_nodedef;
346         ISoundManager *m_sound;
347         MtEventManager *m_event;
348
349         MeshUpdateThread m_mesh_update_thread;
350         ClientEnvironment m_env;
351         con::Connection m_con;
352         IrrlichtDevice *m_device;
353         // Server serialization version
354         u8 m_server_ser_ver;
355         u16 m_playeritem;
356         bool m_inventory_updated;
357         Inventory *m_inventory_from_server;
358         float m_inventory_from_server_age;
359         core::map<v3s16, bool> m_active_blocks;
360         PacketCounter m_packetcounter;
361         // Block mesh animation parameters
362         float m_animation_time;
363         int m_crack_level;
364         v3s16 m_crack_pos;
365         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
366         //s32 m_daynight_i;
367         //u32 m_daynight_ratio;
368         Queue<std::wstring> m_chat_queue;
369         // The seed returned by the server in TOCLIENT_INIT is stored here
370         u64 m_map_seed;
371         std::string m_password;
372         bool m_access_denied;
373         std::wstring m_access_denied_reason;
374         Queue<ClientEvent> m_client_event_queue;
375         FileCache m_media_cache;
376         // Mapping from media file name to SHA1 checksum
377         core::map<std::string, std::string> m_media_name_sha1_map;
378         float m_media_receive_progress;
379         bool m_media_received;
380         bool m_itemdef_received;
381         bool m_nodedef_received;
382         friend class FarMesh;
383
384         // time_of_day speed approximation for old protocol
385         bool m_time_of_day_set;
386         float m_last_time_of_day_f;
387         float m_time_of_day_update_timer;
388
389         // An interval for generally sending object positions and stuff
390         float m_recommended_send_interval;
391
392         // Sounds
393         float m_removed_sounds_check_timer;
394         // Mapping from server sound ids to our sound ids
395         std::map<s32, int> m_sounds_server_to_client;
396         // And the other way!
397         std::map<int, s32> m_sounds_client_to_server;
398         // And relations to objects
399         std::map<int, u16> m_sounds_to_objects;
400
401         // Privileges
402         std::set<std::string> m_privileges;
403
404         // Detached inventories
405         // key = name
406         std::map<std::string, Inventory*> m_detached_inventories;
407 };
408
409 #endif // !CLIENT_HEADER
410