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