4b16b717caf7e8ce26030c4fb30b05b9daec71da
[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 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 #include "inventorymanager.h"
34 #include "filesys.h"
35
36 struct MeshMakeData;
37 class IGameDef;
38 class IWritableTextureSource;
39 class IWritableItemDefManager;
40 class IWritableNodeDefManager;
41 //class IWritableCraftDefManager;
42
43 class ClientNotReadyException : public BaseException
44 {
45 public:
46         ClientNotReadyException(const char *s):
47                 BaseException(s)
48         {}
49 };
50
51 struct QueuedMeshUpdate
52 {
53         v3s16 p;
54         MeshMakeData *data;
55         bool ack_block_to_server;
56
57         QueuedMeshUpdate();
58         ~QueuedMeshUpdate();
59 };
60
61 /*
62         A thread-safe queue of mesh update tasks
63 */
64 class MeshUpdateQueue
65 {
66 public:
67         MeshUpdateQueue();
68
69         ~MeshUpdateQueue();
70         
71         /*
72                 peer_id=0 adds with nobody to send to
73         */
74         void addBlock(v3s16 p, MeshMakeData *data, bool ack_block_to_server);
75
76         // Returned pointer must be deleted
77         // Returns NULL if queue is empty
78         QueuedMeshUpdate * pop();
79
80         u32 size()
81         {
82                 JMutexAutoLock lock(m_mutex);
83                 return m_queue.size();
84         }
85         
86 private:
87         core::list<QueuedMeshUpdate*> m_queue;
88         JMutex m_mutex;
89 };
90
91 struct MeshUpdateResult
92 {
93         v3s16 p;
94         scene::SMesh *mesh;
95         bool ack_block_to_server;
96
97         MeshUpdateResult():
98                 p(-1338,-1338,-1338),
99                 mesh(NULL),
100                 ack_block_to_server(false)
101         {
102         }
103 };
104
105 class MeshUpdateThread : public SimpleThread
106 {
107 public:
108
109         MeshUpdateThread(IGameDef *gamedef):
110                 m_gamedef(gamedef)
111         {
112         }
113
114         void * Thread();
115
116         MeshUpdateQueue m_queue_in;
117
118         MutexedQueue<MeshUpdateResult> m_queue_out;
119
120         IGameDef *m_gamedef;
121 };
122
123 enum ClientEventType
124 {
125         CE_NONE,
126         CE_PLAYER_DAMAGE,
127         CE_PLAYER_FORCE_MOVE,
128         CE_DEATHSCREEN,
129         CE_TEXTURES_UPDATED
130 };
131
132 struct ClientEvent
133 {
134         ClientEventType type;
135         union{
136                 struct{
137                 } none;
138                 struct{
139                         u8 amount;
140                 } player_damage;
141                 struct{
142                         f32 pitch;
143                         f32 yaw;
144                 } player_force_move;
145                 struct{
146                         bool set_camera_point_target;
147                         f32 camera_point_target_x;
148                         f32 camera_point_target_y;
149                         f32 camera_point_target_z;
150                 } deathscreen;
151                 struct{
152                 } textures_updated;
153         };
154 };
155
156 class Client : public con::PeerHandler, public InventoryManager, public IGameDef
157 {
158 public:
159         /*
160                 NOTE: Nothing is thread-safe here.
161         */
162
163         Client(
164                         IrrlichtDevice *device,
165                         const char *playername,
166                         std::string password,
167                         MapDrawControl &control,
168                         IWritableTextureSource *tsrc,
169                         IWritableItemDefManager *itemdef,
170                         IWritableNodeDefManager *nodedef
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         /* InventoryManager interface */
247         Inventory* getInventory(const InventoryLocation &loc);
248         void inventoryAction(InventoryAction *a);
249
250         // Gets closest object pointed by the shootline
251         // Returns NULL if not found
252         ClientActiveObject * getSelectedActiveObject(
253                         f32 max_d,
254                         v3f from_pos_f_on_map,
255                         core::line3d<f32> shootline_on_map
256         );
257
258         // Prints a line or two of info
259         void printDebugInfo(std::ostream &os);
260
261         core::list<std::wstring> getConnectedPlayerNames();
262
263         u32 getDayNightRatio();
264
265         u16 getHP();
266
267         void setTempMod(v3s16 p, NodeMod mod);
268         void clearTempMod(v3s16 p);
269
270         float getAvgRtt()
271         {
272                 try{
273                         return m_con.GetPeerAvgRTT(PEER_ID_SERVER);
274                 } catch(con::PeerNotFoundException){
275                         return 1337;
276                 }
277         }
278
279         bool getChatMessage(std::wstring &message);
280         void typeChatMessage(const std::wstring& message);
281
282         u64 getMapSeed(){ return m_map_seed; }
283
284         void addUpdateMeshTask(v3s16 blockpos, bool ack_to_server=false);
285         // Including blocks at appropriate edges
286         void addUpdateMeshTaskWithEdge(v3s16 blockpos, bool ack_to_server=false);
287
288         // Get event from queue. CE_NONE is returned if queue is empty.
289         ClientEvent getClientEvent();
290         
291         bool accessDenied()
292         { return m_access_denied; }
293
294         std::wstring accessDeniedReason()
295         { return m_access_denied_reason; }
296
297         float textureReceiveProgress()
298         { return m_texture_receive_progress; }
299
300         bool texturesReceived()
301         { return m_textures_received; }
302         bool itemdefReceived()
303         { return m_itemdef_received; }
304         bool nodedefReceived()
305         { return m_nodedef_received; }
306         
307         void afterContentReceived();
308
309         float getRTT(void);
310
311         // IGameDef interface
312         virtual IItemDefManager* getItemDefManager();
313         virtual INodeDefManager* getNodeDefManager();
314         virtual ICraftDefManager* getCraftDefManager();
315         virtual ITextureSource* getTextureSource();
316         virtual u16 allocateUnknownNodeId(const std::string &name);
317
318 private:
319         
320         // Virtual methods from con::PeerHandler
321         void peerAdded(con::Peer *peer);
322         void deletingPeer(con::Peer *peer, bool timeout);
323         
324         void ReceiveAll();
325         void Receive();
326         
327         void sendPlayerPos();
328         // This sends the player's current name etc to the server
329         void sendPlayerInfo();
330         // Send the item number 'item' as player item to the server
331         void sendPlayerItem(u16 item);
332         
333         float m_packetcounter_timer;
334         float m_connection_reinit_timer;
335         float m_avg_rtt_timer;
336         float m_playerpos_send_timer;
337         float m_ignore_damage_timer; // Used after server moves player
338         IntervalLimiter m_map_timer_and_unload_interval;
339
340         IWritableTextureSource *m_tsrc;
341         IWritableItemDefManager *m_itemdef;
342         IWritableNodeDefManager *m_nodedef;
343         MeshUpdateThread m_mesh_update_thread;
344         ClientEnvironment m_env;
345         con::Connection m_con;
346         IrrlichtDevice *m_device;
347         // Server serialization version
348         u8 m_server_ser_ver;
349         u16 m_playeritem;
350         bool m_inventory_updated;
351         Inventory *m_inventory_from_server;
352         float m_inventory_from_server_age;
353         core::map<v3s16, bool> m_active_blocks;
354         PacketCounter m_packetcounter;
355         // Received from the server. 0-23999
356         u32 m_time_of_day;
357         // 0 <= m_daynight_i < DAYNIGHT_CACHE_COUNT
358         //s32 m_daynight_i;
359         //u32 m_daynight_ratio;
360         Queue<std::wstring> m_chat_queue;
361         // The seed returned by the server in TOCLIENT_INIT is stored here
362         u64 m_map_seed;
363         std::string m_password;
364         bool m_access_denied;
365         std::wstring m_access_denied_reason;
366         Queue<ClientEvent> m_client_event_queue;
367         float m_texture_receive_progress;
368         bool m_textures_received;
369         bool m_itemdef_received;
370         bool m_nodedef_received;
371         friend class FarMesh;
372 };
373
374 #endif // !SERVER
375
376 #endif // !CLIENT_HEADER
377