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