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