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